-
Notifications
You must be signed in to change notification settings - Fork 1
/
rtspextr.c
1317 lines (1129 loc) · 29.7 KB
/
rtspextr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2014 Paul Wolneykien <[email protected]>
// Copyright (C) 2014 STC Metrotek [http://metrotek.spb.ru/]
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if defined(UDP) || defined(UNIX)
#include <sys/socket.h>
#endif
#ifdef UDP
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#ifdef UNIX
#include <sys/un.h>
#endif
#include <errno.h>
#include <getopt.h>
#define BUFSIZE 65536
#define DEFIP "127.0.0.1"
#define DEFPORT 5440
#define DEFSOCKDIR "/tmp"
#define DEFSOCKPATH "%s/rtspextr.%s" /* */
#define DEFDUMPDIR "./"
#define DEFDUMPPATH "%s/%s.pcap" /* */
#define DEFRTSPCHN 256
#define DEFMAXCHN 16
#define DEFMAXLEN 2048
#define DEFREPORTCOUNT 1024
#define SENDWHOLE
#ifdef PCAP
#include <pcap/pcap.h>
#include <pcap/bpf.h>
#define PKTBUFSIZE 65536 /* */
#endif
struct params {
#ifdef UDP
char *destip;
#endif
uint16_t destport;
#ifdef UNIX
char *sockdir;
#endif
#ifdef PCAP
char *dumpdir;
#endif
int maxchn;
size_t maxlen;
size_t reportcount;
int dosend;
int ignore_errors;
} varparams = {
#ifdef UDP
NULL,
#endif
DEFPORT,
#ifdef UNIX
NULL,
#endif
#ifdef PCAP
NULL,
#endif
DEFMAXCHN, DEFMAXLEN, DEFREPORTCOUNT, /* */
0, 0,
}; /* */
struct params *params = &varparams; /* */
struct input {
FILE *stream;
#ifdef UDP
int sock;
#endif
size_t pos;
};
struct stats {
size_t rtsp;
size_t rtsp_ok;
size_t tbin;
size_t tbin_b;
size_t chnbin[ DEFRTSPCHN ];
size_t chnbin_b[ DEFRTSPCHN ];
size_t sent;
size_t write_err;
size_t dumped;
size_t other;
int otherflag;
size_t total;
size_t reported;
size_t lastpos;
};
struct binpkt {
char mark;
uint8_t chn;
uint16_t len;
};
enum ptype { BIN, RTSP, EOS, ERR };
struct bufdesc {
uint8_t *buf;
size_t len;
uint8_t *offs;
size_t avail;
};
#ifdef PCAP
struct ethhdr {
uint8_t macdst[ 6 ];
uint8_t macsrc[ 6 ];
uint8_t etype[ 2 ];
};
struct iphdr {
uint8_t ver_ihl;
uint8_t dscp_ecn;
uint16_t iplen;
uint16_t ident;
uint8_t flags_foffs[ 2 ];
uint8_t ttl;
uint8_t proto;
uint16_t hdchksum;
uint8_t ipsrc[ 4 ];
uint8_t ipdst[ 4 ];
};
struct udphdr {
uint8_t srcport[ 2 ];
uint16_t dstport;
uint16_t udplen;
uint16_t udpchksum;
};
struct pkthdr {
struct ethhdr eth;
struct iphdr ip;
struct udphdr udp;
} pkthdr = {
/* Ethernet */
{
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // macdst
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // macsrc
{ 0x08, 0x00 }, // etype
},
/* IPv4 */
{
0x45, // ver.2, header length 20 bytes // ver_ihl
0x00, // dscp_ecn
0x0000, // iplen
0x0000, // ident
{ 0x40, 0x00 }, // don't fragment // flags_foffs
0x40, // 64 // ttl
0x11, // UDP // proto
0x0000, // hdchksum
{ 0x7f, 0x00, 0x00, 0x01 }, // ipsrc
{ 0x7f, 0x00, 0x00, 0x01 }, // ipdst
},
/* UDP */
{
{ 0x02, 0x2a }, // 554 (RTP) // srcport
0x0000, // dstport
0x0000, // udplen
0x0000, // udpchksum
},
};
#endif
struct output {
#if defined(UDP) || defined(UNIX)
int sock;
struct sockaddr *srcaddr;
struct sockaddr *destaddr;
socklen_t addrlen;
#ifdef UDP
struct sockaddr_in destaddr_in;
#endif
#ifdef UNIX
struct sockaddr_un srcaddr_un;
struct sockaddr_un destaddr_un;
#endif
#endif
#ifdef PCAP
pcap_t *pcap;
pcap_dumper_t *dumpers[ DEFRTSPCHN + 1 ];
uint8_t pktbuf[ PKTBUFSIZE ];
#endif
};
int rtspextr( struct input *in, struct output *out,
struct bufdesc *buf, struct stats *stats );
int close_input( struct input *in );
int close_output( struct output *out );
void report_stats( struct stats *stats );
#ifdef UDP
int init_socket_udp( struct output *out );
#endif
#ifdef UNIX
int init_socket_unix( struct output *out );
#endif
#ifdef PCAP
int init_pcap( struct output *out );
#endif
void parse_opts( int argc, char **argv );
void main( int argc, char **argv )
{
uint8_t buf[ BUFSIZE ];
struct bufdesc bufdesc = { buf, sizeof( buf ), buf, 0 };
struct stats stats;
int ret = 0;
parse_opts( argc, argv );
struct input in;
in.stream = stdin;
#ifdef UDP
in.sock = -1;
#endif
in.pos = 0;
struct output out;
#if defined(UDP) || defined(UNIX)
out.sock = -1;
out.srcaddr = NULL;
out.destaddr = NULL;
out.addrlen = 0;
#endif
#ifdef PCAP
out.pcap = NULL;
memset( out.dumpers, 0, sizeof( out.dumpers ) );
#endif
#ifdef UDP
if ( ret == 0 && out.sock < 0 && params->destip ) {
ret = init_socket_udp( &out );
params->dosend = 1;
}
#endif
#ifdef UNIX
if ( ret == 0 && out.sock < 0 && params->sockdir ) {
ret = init_socket_unix( &out );
params->dosend = 1;
}
#endif
#ifdef PCAP
if ( ret == 0 && out.pcap == NULL && params->dumpdir ) {
ret = init_pcap( &out );
params->dosend = 1;
}
#endif
memset( &stats, 0, sizeof( stats ) );
if ( ret == 0 ) {
ret = rtspextr( &in, &out, &bufdesc, &stats );
report_stats( &stats );
}
close_output( &out );
close_input( &in );
exit( ret );
}
extern char *basename (const char *__filename);
void print_help( int argc, char **argv )
{
fprintf( stdout, "Usage %s [ options ]\n", basename( argv[0] ) );
fprintf( stdout, "\n" );
fprintf( stdout, "Options:\n" );
fprintf( stdout, "\n" );
fprintf( stdout, " %-29s %s;\n",
"-h, --help", "print this help screen" );
fprintf( stdout, " %-29s %s;\n",
"-q, --quiet", "output nothing" );
#ifdef UDP
fprintf( stdout, " %-29s %s;\n",
"-u IP:PORT, --udp=IP:PORT",
"send data via UDP to IP:PORT" );
#endif
#ifdef UNIX
fprintf( stdout, " %-29s %s\n%34s%s;\n",
"-U DIR, --unix=DIR",
"send data to the local socket", " ",
"'rtspextr.PORT' in the direcotry DIR" );
#endif
#ifdef PCAP
fprintf( stdout, " %-29s %s\n%34s%s;\n",
"-P DIR, --pcap=DIR", "write libpcap dumps", " ",
"'PORT.pcap' in the directory DIR" );
#endif
fprintf( stdout, " %-29s %s\n%34s%s;\n",
"-C MAXCHN, --maxchn=MAXCHN",
"limit the possible channel number", " ",
"to MAXCHN" );
fprintf( stdout, " %-29s %s\n%34s%s;\n",
"-L MAXLEN, --maxlen=MAXLEN",
"limit the possible packet length", " ",
"to MAXLEN" );
fprintf( stdout, " %-29s %s.\n",
"-R COUNT, --reportevery=COUNT",
"print report every COUNT packets" );
fprintf( stdout, " %-29s %s.\n",
"-e, --ignore-errors",
"ignore the errors" );
}
void parse_opts( int argc, char **argv )
{
int c;
int optidx;
char *endptr;
static struct option opts[] = {
{ "help", no_argument, NULL, 'h' },
{ "quiet", no_argument, NULL, 'q' },
#ifdef UDP
{ "udp", required_argument, NULL, 'u' },
#endif
#ifdef UNIX
{ "unix", required_argument, NULL, 'U' },
#endif
#ifdef PCAP
{ "pcap", required_argument, NULL, 'P' },
#endif
{ "maxchn", required_argument, NULL, 'C' },
{ "maxlen", required_argument, NULL, 'L' },
{ "reportevery", required_argument, NULL, 'R' },
{ "ignore-errors", no_argument, NULL, 'e' },
{ NULL, 0, NULL, 0 }
};
while ( (c = getopt_long( argc, argv, "hqu:U:P:C:L:R:e",
opts, &optidx )) >= 0 )
{
switch (c) {
case 0:
fprintf( stderr, "Unexpected long option: %s\n",
opts[optidx].name );
exit( -1 );
case 'h':
print_help( argc, argv );
exit( 0 );
case 'q':
params->reportcount = 0;
break;;
#ifdef UDP
case 'u':
params->destip = strtok( optarg, ":" );
if ( params->destip == NULL ) {
fprintf( stderr, "Invalid IP:PORT string: %s\n", optarg );
exit( -1 );
}
params->destport =
(uint16_t) strtoul( optarg, &endptr, 10 );
if ( endptr && *endptr != '\0' ) {
fprintf( stderr, "Invalid IP:PORT string: %s\n", optarg );
exit( -1 );
}
break;;
#endif
#ifdef UNIX
case 'U':
params->sockdir = optarg;
break;
#endif
#ifdef PCAP
case 'P':
params->dumpdir = optarg;
break;;
#endif
case 'C':
params->maxchn = (int) strtol( optarg, &endptr, 10 );
if ( endptr && *endptr != '\0' ) {
fprintf( stderr, "Invalid number: %s\n", optarg );
exit( -1 );
}
break;
case 'L':
params->maxlen = (int) strtol( optarg, &endptr, 10 );
if ( endptr && *endptr != '\0' ) {
fprintf( stderr, "Invalid number: %s\n", optarg );
exit( -1 );
}
break;
case 'R':
params->reportcount = (size_t) strtoul( optarg, &endptr, 10 );
if ( endptr && *endptr != '\0' ) {
fprintf( stderr, "Invalid number: %s\n", optarg );
exit( -1 );
}
break;
case 'e':
params->ignore_errors = 1;
break;;
default:
fprintf( stderr, "Try `%s --help` for a biref help page\n",
basename( argv[0] ) );
exit( -1 );
}
}
}
#ifdef UDP
int init_socket_udp( struct output *out )
{
if ( out->sock >= 0 ) {
fprintf( stderr, "Socket already open\n" );
return 1;
}
out->sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if ( out->sock < 0 ) {
fprintf( stderr, "Unable to open output socket\n" );
return 1;
}
out->destaddr_in.sin_family = AF_INET;
out->destaddr_in.sin_port = htons( params->destport );
if ( inet_aton( params->destip, &out->destaddr_in.sin_addr ) == 0 ) {
fprintf( stderr, "Incorrect IP address: %s\n", params->destip );
return 1;
}
out->destaddr = (struct sockaddr *) &out->destaddr_in;
out->addrlen = sizeof( out->destaddr_in );
return 0;
}
#endif
#ifdef UNIX
int init_socket_unix( struct output *out )
{
if ( out->sock >= 0 ) {
fprintf( stderr, "Socket already open\n" );
return 1;
}
int ret = 0;
out->sock = socket( AF_UNIX, SOCK_DGRAM, 0 );
out->destaddr_un.sun_family = AF_UNIX;
out->destaddr = (struct sockaddr *) &out->destaddr_un;
out->addrlen = sizeof( out->destaddr_un );
if ( out->sock < 0 ) {
fprintf( stderr, "Unable to open the socket\n" );
return 1;
}
out->srcaddr_un.sun_family = AF_UNIX;
snprintf( out->srcaddr_un.sun_path,
sizeof( out->srcaddr_un.sun_path ),
"%s%s", params->sockdir, "out" );
unlink( out->srcaddr_un.sun_path );
out->srcaddr = (struct sockaddr *) &out->srcaddr_un;
ret = bind( out->sock, out->srcaddr, out->addrlen );
if ( ret != 0 )
perror( "Unable to bind the socket" );
return ret;
}
#endif
#ifdef PCAP
int init_pcap( struct output *out )
{
if ( out->pcap != NULL ) {
fprintf( stderr, "PCAP already initialized\n" );
return 1;
}
out->pcap = pcap_open_dead( DLT_EN10MB, PKTBUFSIZE );
if ( out->pcap == NULL ) {
fprintf( stderr, "Unable to get the libpcap handle\n" );
return 1;
}
memcpy( out->pktbuf, &pkthdr, sizeof( pkthdr ) );
return 0;
}
#endif
int close_input( struct input *in ) {
int ret = 0;
if ( in->stream ) {
ret = fclose( in->stream );
if ( ret != 0 ) {
fprintf( stderr, "Error closing the input stream\n" );
return ret;
}
in->stream = NULL;
}
#ifdef UDP
if ( in->sock >= 0 ) {
ret = close( in->sock );
if ( ret != 0 ) {
fprintf( stderr, "Error closing the input\n" );
return ret;
}
in->sock = -1;
}
#endif
return 0;
}
int close_output( struct output *out ) {
int i;
int ret = 0;
#if defined(UDP) || defined(UNIX)
if ( out->sock >= 0 ) {
for( i = 0; i <= DEFRTSPCHN; i++ ) {
sendeof( out, i );
}
ret = close( out->sock );
if ( ret != 0 ) {
fprintf( stderr, "Error closing the output socket\n" );
} else {
out->sock = -1;
}
#ifdef UNIX
if ( ret == 0 ) {
if ( out->destaddr->sa_family == AF_UNIX ) {
unlink( ((struct sockaddr_un *)out->srcaddr)->sun_path );
}
}
#endif
}
#endif
#ifdef PCAP
if ( out->pcap ) {
for( i = 0; i <= DEFRTSPCHN; i++ ) {
if ( out->dumpers[ i ] )
pcap_dump_close( out->dumpers[ i ] );
out->dumpers[ i ] = NULL;
}
pcap_close( out->pcap );
}
#endif
return ret;
}
enum ptype find_pkt( struct input *in, struct bufdesc *buf,
struct stats *stats );
int send_bin( struct input *in, struct output *out,
struct bufdesc *buf, struct stats *stats );
int send_rtsp( struct input *in, struct output *out,
struct bufdesc *buf, struct stats *stats );
int rtspextr( struct input *in, struct output *out,
struct bufdesc *buf, struct stats *stats )
{
int ret = 0;
while ( ret == 0 ) {
if ( stats->total - stats->reported >= params->reportcount ) {
report_stats( stats );
}
enum ptype ptype = find_pkt( in, buf, stats );
switch ( ptype ) {
case BIN:
stats->total++;
stats->tbin++;
ret = send_bin( in, out, buf, stats );
if ( ret != 0 ) {
fprintf( stderr, "Unable to read/send binary packet\n" );
if ( params->ignore_errors ) {
fprintf( stderr, "Error ignored\n" );
ret = 0;
}
}
break;
case RTSP:
stats->total++;
stats->rtsp++;
ret = send_rtsp( in, out, buf, stats );
if ( ret != 0 ) {
fprintf( stderr, "Unable to read/send RTSP packet\n" );
if ( params->ignore_errors ) {
fprintf( stderr, "Error ignored\n" );
ret = 0;
}
}
break;
case EOS:
ret = 127;
break;
case ERR:
ret = 255;
}
}
if ( ret == 127 )
ret = 0;
return ret;
}
void report_stats( struct stats *stats )
{
int i;
if ( params->reportcount == 0 ) {
stats->reported = stats->total;
return;
}
fprintf( stdout, "DEC Total packets detected: %lu\n",
stats->total );
fprintf( stdout, "RTSP RTSP packets detected: %lu\n",
stats->rtsp );
fprintf( stdout, "RTSPOK Having '200 OK' status: %lu\n",
stats->rtsp_ok );
for ( i = 0; i < DEFRTSPCHN; i++ ) {
if ( stats->chnbin[ i ] ) {
fprintf( stdout, "BIN.%i Channel #%i binary packets detected: %lu\n",
i, i, stats->chnbin[ i ] );
fprintf( stdout, "BINB.%i Channel #%i Binary traffic, bytes: %lu\n",
i, i, stats->chnbin_b[ i ] );
}
}
fprintf( stdout, "TBIN Total binary packets detected: %lu\n",
stats->tbin );
fprintf( stdout, "TBINB Total binary traffic, bytes: %lu\n",
stats->tbin_b );
fprintf( stdout, "SENT Packets sent: %lu\n",
stats->sent );
if ( params->dosend ) {
fprintf( stdout, "ERR Send/dump errors: %lu\n",
stats->write_err );
}
fprintf( stdout, "UNDET Uknown traffic, bytes: %lu\n",
stats->other );
fprintf( stdout, "\n" );
stats->reported = stats->total;
}
int read_next( struct input *in, struct bufdesc *buf )
{
if ( buf->avail > 0 && buf->offs > buf->buf ) {
memmove( buf->buf, buf->offs, buf->avail );
}
buf->offs = buf->buf + buf->avail;
size_t toread = buf->len - ((size_t) (buf->offs - buf->buf));
size_t rd;
if ( in->stream ) {
if ( feof ( in->stream ) )
return 127;
rd = fread( buf->offs, 1, toread, in->stream );
if ( rd < toread ) {
if ( ferror( in->stream ) ) {
fprintf( stderr, "I/O stream read error\n" );
return 255;
}
}
#ifdef UDP
} else if ( in->sock >= 0 ) {
// TODO: implement packet reading (1 packet at a time)
fprintf( stderr, "Socket input isn't implemented yet\n" );
return 255;
#endif
}
buf->offs = buf->buf;
buf->avail += rd;
in->pos += rd;
return 0;
}
int skip( struct bufdesc *buf, size_t toskip )
{
if ( toskip > buf->avail ) {
fprintf( stderr, "Unable to skip %lu bytes: only %lu is " \
"available\n",
toskip, buf->avail );
return 1;
}
buf->offs += toskip;
buf->avail -= toskip;
return 0;
}
int unskip( struct bufdesc *buf, size_t tounskip )
{
if ( (buf->offs - tounskip) < buf->buf ) {
fprintf( stderr, "Unable to unskip %lu bytes: only %lu is " \
"available\n",
tounskip, (size_t) (buf->offs - buf->buf) );
return 1;
}
buf->offs -= tounskip;
buf->avail += tounskip;
return 0;
}
int peek_bin_header( struct input *in, struct bufdesc *buf,
struct binpkt *hdr );
enum ptype find_pkt( struct input *in, struct bufdesc *buf,
struct stats *stats )
{
int ret = 0;
while ( ret == 0 ) {
while ( buf->avail >= 4 ) {
if ( *buf->offs == 0x24 ) {
struct binpkt binhdr;
if ( peek_bin_header( in, buf, &binhdr ) != 0 )
return ERR;
if ( binhdr.chn <= params->maxchn &&
binhdr.len <= params->maxlen )
{
stats->otherflag = 0;
stats->lastpos = in->pos - buf->avail;
return BIN;
}
}
if ( strncmp( (char *) buf->offs, "RTSP", 4 ) == 0 ) {
stats->otherflag = 0;
stats->lastpos = in->pos - buf->avail;
return RTSP;
}
if ( !stats->otherflag ) {
stats->otherflag = 1;
fprintf( stderr, "Unknown data at %08lx + %02lx, " \
"last packet at %08lx + %02lx\n",
((in->pos - buf->avail) / 16) * 16,
(in->pos - buf->avail) % 16,
(stats->lastpos / 16) * 16,
stats->lastpos % 16 );
}
stats->other++;
if ( skip( buf, 1 ) != 0 )
return ERR;
}
ret = read_next( in, buf );
}
if ( ret == 127 )
return EOS;
else
return ERR;
}
int read_ahead( struct input *in, struct bufdesc *buf )
{
if ( buf->offs > buf->buf ) {
return read_next( in, buf );
} else {
return 0;
}
}
int peek_bin_header( struct input *in, struct bufdesc *buf,
struct binpkt *hdr )
{
int ret = 0;
if ( buf->avail < 4 ) {
ret = read_ahead( in, buf );
if ( ret != 0 ) {
fprintf( stderr, "Unable to read the interleaved packet " \
"header\n" );
return ret;
}
}
if ( buf->avail < 4 ) {
fprintf( stderr, "Need %lu more bytes to identify binary packet\n",
4 - buf->avail );
return 1;
}
hdr->mark = (char) *buf->offs;
hdr->chn = *(buf->offs + 1);
hdr->len = ntohs( *((uint16_t *) (buf->offs + 2)) );
return 0;
}
int read_bin_header( struct input *in, struct bufdesc *buf,
struct binpkt *hdr )
{
int ret = 0;
ret = peek_bin_header( in, buf, hdr );
if ( ret != 0 )
return ret;
if ( skip( buf, 4 ) != 0 )
return 1;
return 0;
}
int setoutport( struct output *out, int chn )
{
struct sockaddr_in *destaddr_in;
struct sockaddr_un *destaddr_un;
char suff[ 5 ];
static char dumppath[ 1024 ];
#if defined(UDP) || defined(UNIX)
if ( out->destaddr ) {
switch ( out->destaddr->sa_family ) {
#ifdef UDP
case AF_INET:
destaddr_in = (struct sockaddr_in *) out->destaddr;
destaddr_in->sin_port = htons( params->destport + chn );
break;
#endif
#ifdef UNIX
case AF_UNIX:
destaddr_un = (struct sockaddr_un *) out->destaddr;
if ( chn != DEFRTSPCHN )
snprintf( suff, 5, "%i", chn );
else
snprintf( suff, 5, "%s", "rtsp" );
snprintf( destaddr_un->sun_path,
sizeof( destaddr_un->sun_path ),
DEFSOCKPATH, params->sockdir, suff );
break;
#endif
default:
fprintf( stderr, "Unsupported destination address family: %i\n",
out->destaddr->sa_family );
return 1;
}
}
#endif
#ifdef PCAP
if ( out->pcap ) {
if ( out->dumpers[ chn ] == NULL ) {
if ( chn != DEFRTSPCHN )
snprintf( suff, 5, "%i", chn );
else
snprintf( suff, 5, "%s", "rtsp" );
snprintf( dumppath, sizeof( dumppath ), DEFDUMPPATH,
params->dumpdir, suff );
out->dumpers[ chn ] = pcap_dump_open( out->pcap, dumppath );
if ( out->dumpers[ chn ] == NULL ) {
fprintf( stderr, "Unable to create the dumpfile %s\n",
dumppath );
return 1;
}
}
struct pkthdr *bufhdr = (struct pkthdr *) out->pktbuf;
bufhdr->udp.dstport = htons( params->destport + chn );
}
#endif
return 0;
}
#if defined(UDP) || defined(UNIX)
size_t sendout( struct output *out, int chn,
const void *buf, size_t towrite, int send )
{
int flags = 0;
if ( !send )
flags |= MSG_MORE;
size_t wt = sendto( out->sock, buf, towrite, flags,
(struct sockaddr *) out->destaddr,
out->addrlen );
if ( ((int) wt) < 0 ) {
if ( errno == ENOENT )
wt = 0;
}
return wt;
}
#endif
#ifdef PCAP
size_t dump( struct output *out, int chn,
const void *buf, size_t towrite, int complete )
{
size_t ret = 0;
if ( !complete ) {
fprintf( stderr, "Can't dump incomplete packet\n" );
errno = ECANCELED;
ret = -1;
} else {
if ( sizeof( pkthdr ) + towrite > PKTBUFSIZE ) {
fprintf( stderr, "Packet dump buffer too small\n" );
ret = -1;
} else {
struct pkthdr *bufhdr = (struct pkthdr *) out->pktbuf;
bufhdr->ip.iplen = htons( sizeof( struct pkthdr )
- sizeof( struct ethhdr )
+ towrite );
bufhdr->udp.udplen = htons( sizeof( struct pkthdr )
- sizeof( struct ethhdr )
- sizeof( struct iphdr )
+ towrite );
memcpy( out->pktbuf + sizeof( pkthdr ), buf, towrite );
struct pcap_pkthdr phdr;
phdr.caplen = sizeof( pkthdr ) + towrite;
phdr.len = phdr.caplen;
gettimeofday( &phdr.ts, NULL );
pcap_dump( (u_char *) out->dumpers[ chn ], &phdr, out->pktbuf );
ret = towrite;
}
}
return ret;
}
#endif
size_t writeout( struct output *out, int chn,
const void *buf, size_t towrite, int complete,
struct stats *stats )
{
size_t ret = 0;
if ( setoutport( out, chn ) != 0 )
return -1;
#if defined(UDP) || defined(UNIX)