├── src ├── VERSION ├── Makefile ├── net.h ├── ieee80211.h ├── bpf.h ├── net.c ├── main.h ├── common.h ├── rtp_pt.h ├── rtp.h ├── common.c ├── queue.h └── main.c ├── .travis.yml ├── Makefile ├── README.md └── LICENSE /src/VERSION: -------------------------------------------------------------------------------- 1 | VERSION=1.3r 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | compiler: 4 | - gcc 5 | 6 | install: 7 | - sudo apt-get update || true 8 | - sudo apt-get install build-essential 9 | - sudo apt-get install libpcap-dev libnet-dev ffmpeg gnuplot 10 | 11 | script: 12 | - make 13 | - ./rtpbreakr 14 | 15 | notifications: 16 | slack: qxip:WuEGMSIAa8KEX6iL2zN2bZbv 17 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # rtpbreakr Makefile. 2 | 3 | INSTALL_DIR = "/usr/bin/" 4 | 5 | ##################################################################### 6 | 7 | 8 | all: build 9 | 10 | build: 11 | @cd src ; $(MAKE) 12 | mv src/rtpbreakr ./ 13 | @echo "" 14 | @echo "rtpbreakr has been compiled!" 15 | @echo "" 16 | 17 | 18 | install: 19 | cp ./rtpbreakr $(INSTALL_DIR) 20 | @echo "" 21 | @echo "rtpbreakr has been installed!" 22 | @echo "" 23 | 24 | clean: 25 | cd src ; $(MAKE) clean 26 | rm -rf ./rtpbreakr 27 | #eof 28 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | # src Makefile. 2 | 3 | include VERSION 4 | 5 | CC = cc 6 | CFLAGS = -Wall -O3 # debug: put -ggdb instead of -O3 7 | LDFLAGS = 8 | LIBS = -lpcap -lnet 9 | DEFS = 10 | 11 | ##################################################################### 12 | 13 | 14 | all: header 15 | $(CC) -c -DVERSION=\"$(VERSION)\" $(CFLAGS) $(DEFS) main.c 16 | $(CC) -c $(CFLAGS) $(DEFS) common.c 17 | $(CC) -c $(CFLAGS) $(DEFS) net.c 18 | $(CC) $(LDFLAGS) main.o common.o net.o -o rtpbreakr $(LIBS) 19 | @echo "%" 20 | 21 | header: 22 | @echo "%" 23 | @echo "% Compiling rtpbreakr v$(VERSION)" 24 | @echo "%" 25 | @echo "% CC...................: $(CC)" 26 | @echo "% CFLAGS...............: $(CFLAGS)" 27 | @echo "% LDFLAGS..............: $(LDFLAGS)" 28 | @echo "% LIBS.................: $(LIBS)" 29 | @echo "% DEFS.................: $(DEFS)" 30 | @echo "%" 31 | 32 | indent: 33 | astyle --convert-tabs --style=gnu *.[hc] 34 | 35 | srcheaders: 36 | update_src_header.sh *h *c 37 | 38 | clean: 39 | @rm -f rtpbreakr *.[hc].orig rtp.*.txt rtp.*.*.* *.o *.wav *~~ 40 | 41 | #eof 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ![RTPbreakR](http://i.imgur.com/CztZLDE.png) 2 | 3 | [![Build Status](https://travis-ci.org/sipcapture/rtpbreakr.svg?branch=master)](https://travis-ci.org/sipcapture/rtpbreakr) 4 | 5 | ### Requirements: 6 | 7 | * Compile: 8 | * ```libpcap-dev```, ```libnet-dev``` 9 | * Runtime: 10 | * ```ffmpeg 2.x``` and ```gnuplot 4.4``` installed on the target system 11 | 12 | NOTE: For older ffmpeg versions (0.x) use [this version](https://github.com/sipcapture/rtpbreakr/tree/oldffmpeg) 13 | 14 | ### Usage 15 | ``` 16 | $ rtpbreakr (-r|-i) [options] 17 | ``` 18 | 19 | ### Example with n2disk Timeline 20 | ``` 21 | $ ./npcapextract -t /tmp/n2disk/timeline -b "2015-10-18 12:10:00" -e "2015-10-18 12:20:00" -o /path/to/rtp.pcap -f "((udp) and ((port 7800) or (port 32402)) and ((host 192.168.1.254) or (host 192.168.1.200)))” 22 | $ ./rtpbreakr -r /path/to/rtp.pcap -d /tmp 23 | ``` 24 | 25 | ### Output: 26 | ``` 27 | /tmp/rtp.0.1.g711A 28 | /tmp/rtp.0.1.txt 29 | /tmp/rtp.0.1.mp3 30 | /tmp/rtp.0.1.mp3.png 31 | /tmp/rtp.0.2.g711A 32 | /tmp/rtp.0.2.txt 33 | /tmp/rtp.0.2.mp3 34 | /tmp/rtp.0.2.mp3.png 35 | /tmp/rtp.0.html 36 | /tmp/rtp.0.txt 37 | 38 | ``` 39 | ![mugshot](http://i.imgur.com/AnsJPOV.png) 40 | -------------------------------------------------------------------------------- /src/net.h: -------------------------------------------------------------------------------- 1 | /* 2 | * net.h by xenion -- 2008-05-05 -- v.9f90fb024b189a85013c576f412984a6 3 | * 4 | * Copyright (c) 2007-2008 Dallachiesa Michele 5 | * 6 | * 7 | * This program is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU General Public License 9 | * as published by the Free Software Foundation; either version 2 10 | * of the License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | * 21 | */ 22 | 23 | #ifndef NET_H 24 | #define NET_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include "bpf.h" 33 | #include "ieee80211.h" 34 | 35 | /* macros */ 36 | 37 | #define INET_NTOA(x) (inet_ntoa(*((struct in_addr *)&(x)))) 38 | 39 | #define SAFE_PCAP_CLOSE(x) do { if (x) { pcap_close(x); x = NULL; }}while(0) 40 | #define SAFE_LIBNET_CLOSE(x) do { if (x) { libnet_destroy(x); x = NULL; }}while(0) 41 | 42 | 43 | /* types */ 44 | 45 | struct pcap_pkt 46 | { 47 | struct pcap_pkthdr hdr; 48 | u_int8_t *pkt; 49 | u_int16_t dllength; 50 | u_int16_t dlltype; 51 | } ; 52 | 53 | 54 | /* protos */ 55 | 56 | extern int sizeof_datalink(pcap_t * p); 57 | extern void add_pcap_filter(pcap_t *p, char *s); 58 | 59 | 60 | #endif 61 | 62 | /* EOF */ 63 | 64 | -------------------------------------------------------------------------------- /src/ieee80211.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ieee80211.h by xenion -- 2008-05-05 -- v.c486a4662d73aaca28a52ba95febd8b3 3 | * 4 | * Copyright (c) 2007-2008 Dallachiesa Michele 5 | * 6 | * 7 | * This program is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU General Public License 9 | * as published by the Free Software Foundation; either version 2 10 | * of the License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | * 21 | */ 22 | 23 | // adapted from: 24 | // $FreeBSD: src/sys/net80211/ieee80211.h,v 1.9.2.2 2006/08/10 06:07:49 sam Exp $ 25 | 26 | /* does frame have QoS sequence control data */ 27 | #define IEEE80211_QOS_HAS_SEQ(wh) \ 28 | (((wh)->i_fc[0] & \ 29 | (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_QOS)) == \ 30 | (IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_QOS)) 31 | 32 | #define IEEE80211_ADDR_LEN 6 /* size of 802.11 address */ 33 | #define IEEE80211_FC0_TYPE_MASK 0x0c 34 | #define IEEE80211_FC0_TYPE_DATA 0x08 35 | #define IEEE80211_FC1_DIR_MASK 0x03 36 | #define IEEE80211_FC1_DIR_DSTODS 0x03 /* AP ->AP */ 37 | #define IEEE80211_FC0_SUBTYPE_MASK 0xf0 38 | #define IEEE80211_FC0_SUBTYPE_QOS 0x80 39 | 40 | struct ieee80211_frame 41 | { 42 | u_int8_t i_fc[2]; 43 | u_int8_t i_dur[2]; 44 | u_int8_t i_addr1[ETHER_ADDR_LEN]; 45 | u_int8_t i_addr2[ETHER_ADDR_LEN]; 46 | u_int8_t i_addr3[ETHER_ADDR_LEN]; 47 | u_int8_t i_seq[2]; 48 | /* possibly followed by addr4[ETHER_ADDR_LEN]; */ 49 | }; 50 | 51 | 52 | /* EOF */ 53 | 54 | 55 | -------------------------------------------------------------------------------- /src/bpf.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * This code is derived from the Stanford/CMU enet packet filter, 6 | * (net/enet.c) distributed as part of 4.3BSD, and code contributed 7 | * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 8 | * Berkeley Laboratory. 9 | * 10 | * Redistribution and use in source and binary forms, with or without 11 | * modification, are permitted provided that the following conditions 12 | * are met: 13 | * 1. Redistributions of source code must retain the above copyright 14 | * notice, this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 3. All advertising materials mentioning features or use of this software 19 | * must display the following acknowledgement: 20 | * This product includes software developed by the University of 21 | * California, Berkeley and its contributors. 22 | * 4. Neither the name of the University nor the names of its contributors 23 | * may be used to endorse or promote products derived from this software 24 | * without specific prior written permission. 25 | * 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 | * SUCH DAMAGE. 37 | * 38 | * @(#)bpf.h 7.1 (Berkeley) 5/7/91 39 | * 40 | */ 41 | 42 | 43 | #define AP_DLT_NULL 0 /* no link-layer encapsulation */ 44 | #define AP_DLT_EN10MB 1 /* Ethernet (10Mb) */ 45 | #define AP_DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */ 46 | #define AP_DLT_AX25 3 /* Amateur Radio AX.25 */ 47 | #define AP_DLT_PRONET 4 /* Proteon ProNET Token Ring */ 48 | #define AP_DLT_CHAOS 5 /* Chaos */ 49 | #define AP_DLT_IEEE802 6 /* IEEE 802 Networks */ 50 | #define AP_DLT_ARCNET 7 /* ARCNET */ 51 | #define AP_DLT_SLIP 8 /* Serial Line IP */ 52 | #define AP_DLT_PPP 9 /* Point-to-point Protocol */ 53 | #define AP_DLT_FDDI 10 /* FDDI */ 54 | #define AP_DLT_ATM_RFC1483 11 /* LLC/SNAP encapsulated atm */ 55 | 56 | #if defined(__OpenBSD__) 57 | #define AP_DLT_LOOP 12 /* old DLT_LOOP interface :4 byte offset */ 58 | #define AP_DLT_RAW 14 /* raw IP: 0 byte offset */ 59 | #else 60 | #define AP_DLT_RAW 12 /* raw IP: 0 byte offset*/ 61 | #define AP_DLT_LOOP 108 62 | #endif 63 | 64 | #define AP_DLT_ENC 13 65 | #define AP_DLT_SLIP_BSDOS 15 /* BSD/OS Serial Line IP */ 66 | #define AP_DLT_PPP_BSDOS 16 /* BSD/OS Point-to-point Protocol */ 67 | #define AP_DLT_ATM_CLIP 19 /* Linux Classical-IP over ATM */ 68 | #define AP_DLT_PPP_SERIAL 50 /* PPP over serial with HDLC encapsulation */ 69 | #define AP_DLT_PPP_ETHER 51 /* PPP over Ethernet */ 70 | #define AP_DLT_C_HDLC 104 /* Cisco HDLC */ 71 | #define AP_DLT_CHDLC DLT_C_HDLC 72 | #define AP_DLT_IEEE802_11 105 /* IEEE 802.11 wireless */ 73 | #define AP_DLT_LINUX_SLL 113 74 | #define AP_DLT_LTALK 114 75 | #define AP_DLT_ECONET 115 76 | #define AP_DLT_IPFILTER 116 77 | #define AP_DLT_PFLOG 117 78 | #define AP_DLT_CISCO_IOS 118 79 | #define AP_DLT_PRISM_HEADER 119 80 | #define AP_DLT_AIRONET_HEADER 120 81 | -------------------------------------------------------------------------------- /src/net.c: -------------------------------------------------------------------------------- 1 | /* 2 | * net.c by xenion -- 2008-05-05 -- v.0e2f795ca3b9af8bf863598b0a729ec4 3 | * 4 | * Copyright (c) 2007-2008 Dallachiesa Michele 5 | * 6 | * 7 | * This program is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU General Public License 9 | * as published by the Free Software Foundation; either version 2 10 | * of the License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | * 21 | */ 22 | 23 | 24 | #include 25 | #include 26 | #include "bpf.h" 27 | #include "common.h" 28 | #include "net.h" 29 | 30 | 31 | /* macros */ 32 | 33 | #define CASE(x,y) { case (x): return y; break; } 34 | 35 | 36 | /*******************************************/ 37 | 38 | 39 | int 40 | sizeof_datalink(pcap_t * p) 41 | { 42 | int dtl; 43 | 44 | 45 | if ((dtl = pcap_datalink(p)) < 0) 46 | FATAL("pcap_datalink(): %s", pcap_geterr(p)); 47 | 48 | switch (dtl) 49 | { 50 | 51 | CASE(AP_DLT_NULL, 4); 52 | CASE(AP_DLT_EN10MB, 14); 53 | CASE(AP_DLT_EN3MB, 14); 54 | CASE(AP_DLT_AX25, -1); 55 | CASE(AP_DLT_PRONET, -1); 56 | CASE(AP_DLT_CHAOS, -1); 57 | CASE(AP_DLT_IEEE802, 22); 58 | CASE(AP_DLT_ARCNET, -1); 59 | #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__NetBSD__) || defined (__BSDI__) 60 | 61 | CASE(AP_DLT_SLIP, 16); 62 | #else 63 | 64 | CASE(AP_DLT_SLIP, 24); 65 | #endif 66 | 67 | #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__NetBSD__) 68 | 69 | CASE(AP_DLT_PPP, 4); 70 | #elif defined (__sun) 71 | 72 | CASE(AP_DLT_PPP, 8); 73 | #else 74 | 75 | CASE(AP_DLT_PPP, 24); 76 | #endif 77 | 78 | CASE(AP_DLT_FDDI, 21); 79 | CASE(AP_DLT_ATM_RFC1483, 8); 80 | 81 | CASE(AP_DLT_LOOP, 4); /* according to OpenBSD DLT_LOOP */ 82 | CASE(AP_DLT_RAW, 0); 83 | 84 | CASE(AP_DLT_SLIP_BSDOS, 16); 85 | CASE(AP_DLT_PPP_BSDOS, 4); 86 | CASE(AP_DLT_ATM_CLIP, -1); 87 | #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__NetBSD__) 88 | 89 | CASE(AP_DLT_PPP_SERIAL, 4); 90 | CASE(AP_DLT_PPP_ETHER, 4); 91 | #elif defined (__sun) 92 | 93 | CASE(AP_DLT_PPP_SERIAL, 8); 94 | CASE(AP_DLT_PPP_ETHER, 8); 95 | #else 96 | 97 | CASE(AP_DLT_PPP_SERIAL, 24); 98 | CASE(AP_DLT_PPP_ETHER, 24); 99 | #endif 100 | 101 | CASE(AP_DLT_C_HDLC, -1); 102 | CASE(AP_DLT_IEEE802_11, 30); 103 | CASE(AP_DLT_LINUX_SLL, 16); 104 | CASE(AP_DLT_LTALK, -1); 105 | CASE(AP_DLT_ECONET, -1); 106 | CASE(AP_DLT_IPFILTER, -1); 107 | CASE(AP_DLT_PFLOG, -1); 108 | CASE(AP_DLT_CISCO_IOS, -1); 109 | CASE(AP_DLT_PRISM_HEADER, -1); 110 | CASE(AP_DLT_AIRONET_HEADER, -1); 111 | 112 | default: 113 | FATAL("unknown datalink type DTL_?=%d", dtl); 114 | break; 115 | } 116 | 117 | return 0; 118 | } 119 | 120 | 121 | void 122 | add_pcap_filter(pcap_t *p, char *s) 123 | { 124 | struct bpf_program bpf_filter; 125 | 126 | if (!s) 127 | { 128 | LOG(1,1," ! The pcap filter is NULL, ignored"); 129 | return; 130 | } 131 | 132 | // LOG(1,1," * Adding pcap_filter: '%s'", s); 133 | 134 | if (pcap_compile(p, &bpf_filter, s, 0, 0) < 0) 135 | FATAL("pcap_compile(): %s", pcap_geterr(p)); 136 | 137 | if (pcap_setfilter(p, &bpf_filter) < 0) 138 | FATAL("pcap_setfilter(): %s", pcap_geterr(p)); 139 | 140 | pcap_freecode(&bpf_filter); 141 | } 142 | 143 | 144 | /* EOF */ 145 | 146 | 147 | -------------------------------------------------------------------------------- /src/main.h: -------------------------------------------------------------------------------- 1 | /* 2 | * main.h by xenion -- 2008-05-05 -- v.50ea4697a08b7fa64400295ae63b67a1 3 | * 4 | * Copyright (c) 2007-2008 Dallachiesa Michele 5 | * 6 | * 7 | * This program is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU General Public License 9 | * as published by the Free Software Foundation; either version 2 10 | * of the License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | * 21 | */ 22 | 23 | 24 | /* const */ 25 | 26 | #define DEFAULT_OUTDIR "." 27 | 28 | #define RTP_SEQWINDOW 200 29 | #define RTP_TSWINDOW (500 * RTP_STREAM_PATTERN_PKTS) 30 | #define RTP_STREAM_PATTERN_PKTS 5 31 | 32 | // se un pkt sta nel buffer un tempo > di PKT_TIMEOUT, viene flushato 33 | // se non arrivano pkt per un tempo > di PKT_TIMEOUT, la sessione viene 34 | // considerata conclusa. 35 | #define PKT_TIMEOUT 10.0 36 | #define RTP_STREAM_PATTERN_TIMEOUT 0.25 37 | 38 | 39 | /* types */ 40 | 41 | typedef struct 42 | { 43 | int verbose; 44 | char *rxfile; 45 | char *iface; 46 | char *outdir; 47 | int dllength; 48 | int fill_gaps; 49 | int rtp_hdr_pt; 50 | int udp_hdr_even_dst_port; 51 | int udp_hdr_unpriv_ports; 52 | char *mypcap_filter; 53 | float timeout_pkt; 54 | float timeout_pattern; 55 | int pattern_pkts; 56 | int rtp_payload_length; 57 | int dump_noise; 58 | char *user; 59 | int daemonize; 60 | int promisc; 61 | int syslog; 62 | int stdout; 63 | int dump_raw; 64 | int dump_pcap; 65 | int dump_wav; 66 | FILE *player; 67 | } 68 | OPT; 69 | 70 | 71 | typedef struct 72 | { 73 | struct pcap_pkt pcap; 74 | u_int32_t len; // length of udp payload (rtp header, extension and codec data). 75 | u_int32_t hdroff; // offset to reach the first udp byte, the rtp header. 76 | struct 77 | { 78 | u_int32_t off; 79 | u_int32_t len; 80 | } 81 | payload; 82 | } 83 | pktrtp_t; 84 | 85 | 86 | struct rtpbuf_entry 87 | { 88 | pktrtp_t pktrtp; 89 | LIST_ENTRY(rtpbuf_entry) l; 90 | }; 91 | 92 | 93 | LIST_HEAD(rtpbuf_head, rtpbuf_entry); 94 | 95 | 96 | typedef struct 97 | { 98 | #define ADDRS_TYPE_UNKNOWN 0 99 | #define ADDRS_TYPE_IP 1 100 | #define ADDRS_TYPE_TCP 2 101 | #define ADDRS_TYPE_UDP 3 102 | int type; 103 | u_int32_t srcaddr; 104 | u_int32_t dstaddr; 105 | u_int16_t srcport; 106 | u_int16_t dstport; 107 | } 108 | addrs_t; 109 | 110 | 111 | struct rtp_stream_entry 112 | { 113 | int fid; 114 | int id; 115 | pcap_dumper_t *pdump; 116 | pcap_dumper_t *noise; 117 | FILE *f; 118 | FILE *raw; 119 | addrs_t addrs; 120 | u_int32_t ssrc; 121 | u_int32_t max_ts_seen; // max last timestamp seen 122 | u_int16_t max_seq_seen; // max last sequence seen 123 | u_int16_t last_seq_flhd; // last sequence flushed 124 | struct rtpbuf_head pkts; // buffered rtp packets 125 | struct timeval last_pkt; 126 | struct timeval first_pkt; 127 | u_int32_t pktcount_flhd; 128 | u_int32_t pktcount_inbuf; 129 | u_int32_t pktcount_lost; 130 | int pattern_found; 131 | int payload_type; 132 | u_int32_t last_payload_length; 133 | int payload_length_fixed; 134 | LIST_ENTRY(rtp_stream_entry) l; // list link 135 | struct rtp_stream_entry *rev; 136 | char command[1024]; 137 | }; 138 | 139 | 140 | LIST_HEAD(rtp_streams_head, rtp_stream_entry); 141 | 142 | 143 | struct rtp_streams_list 144 | { 145 | u_int32_t max_id; 146 | int32_t nclosed; // pattern not found and closed 147 | int32_t closed; // pattern found and closed 148 | int32_t active; // active and pattern found 149 | u_int32_t pktcount_lost; 150 | u_int32_t pktcount_noise; 151 | u_int32_t pktcount; 152 | struct rtp_streams_head list; 153 | }; 154 | 155 | 156 | /* EOF */ 157 | 158 | -------------------------------------------------------------------------------- /src/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * common.h by xenion -- 2008-05-05 -- v.1293c16fff21c9e111936bd906f13e1c 3 | * 4 | * Copyright (c) 2007-2008 Dallachiesa Michele 5 | * 6 | * 7 | * This program is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU General Public License 9 | * as published by the Free Software Foundation; either version 2 10 | * of the License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | * 21 | */ 22 | 23 | #ifndef COMMON_H 24 | #define COMMON_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | 35 | /* const */ 36 | 37 | #define LINE_BUFFER_MAX 4096 38 | 39 | #ifndef PATH_MAX 40 | #define PATH_MAX 4096 41 | #endif 42 | 43 | 44 | /* macros */ 45 | 46 | // returns 1 if a > b, -1 if a < b, 0 if a == b 47 | #define TIMEVAL_CMP(a, b) ( \ 48 | a.tv_sec > b.tv_sec ? 1 : \ 49 | a.tv_sec < b.tv_sec ? -1 : \ 50 | a.tv_usec > b.tv_usec ? 1 : \ 51 | a.tv_usec < b.tv_usec ? -1 : 0 ) 52 | 53 | #define TIMEVAL_SUB(a, b, result) \ 54 | do { \ 55 | (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ 56 | (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \ 57 | if ((result)->tv_usec < 0) { \ 58 | --(result)->tv_sec; \ 59 | (result)->tv_usec += 1000000; \ 60 | } \ 61 | } while (0) 62 | 63 | #define MAX(x,y) ( (x) > (y) ? (x) : (y)) 64 | #define MIN(x,y) ( (x) < (y) ? (x) : (y)) 65 | 66 | #define PERCENTAGE(x,y) ((y) == 0 ? 0 : (float)(x) * 100 / (y)) 67 | 68 | #define LOG(h,n,fmt, ...) do { \ 69 | logthis(__FILE__, __FUNCTION__, __LINE__,0,h,n,fmt, ## __VA_ARGS__ ); \ 70 | } while(0) 71 | 72 | #define VLOG(h,n,fmt, ...) do { \ 73 | logthis(__FILE__, __FUNCTION__, __LINE__,1,h,n,fmt, ## __VA_ARGS__ ); \ 74 | } while(0) 75 | 76 | #define FATAL(x, ...) do { \ 77 | fatal(__FILE__, __FUNCTION__, __LINE__,x, ## __VA_ARGS__ ); \ 78 | } while(0) 79 | 80 | #define SAFE_FREE(x) do { if (x) { free(x); x = NULL; }}while(0) 81 | #define SAFE_CLOSE(x) do { if(x != -1) { close(x) ; x = -1;}}while(0) 82 | #define SAFE_FCLOSE(x) do { if (x) { fclose(x); x = NULL; }}while(0) 83 | #define SAFE_FPRINTF(x, ...) do { if(x) fprintf(x, ## __VA_ARGS__ ); } while(0) 84 | #define SAFE_PDCLOSE(x) do { if (x) { pcap_dump_close(x); x = NULL; }}while(0) 85 | #define SAFE_STRDUP(x) (*x ? strdup(x) : NULL) 86 | 87 | #define SWITCH_VALUES(x,y,tmp) do { tmp=x; x=y; y=tmp; } while(0) 88 | 89 | #define STATIC_STRLEN(x) (sizeof(x)-1) // (sizeof("ciao")-1) == 4 90 | 91 | #define SIG_NAME(x) x == SIGURG ? "SIGURG" : \ 92 | x == SIGPIPE ? "SIGPIPE" : \ 93 | x == SIGQUIT ? "SIGQUIT" : \ 94 | x == SIGINT ? "SIGINT" : \ 95 | x == SIGTERM ? "SIGTERM" : \ 96 | x == SIGHUP ? "SIGHUP" : \ 97 | x == SIGSEGV ? "SIGSEGV" : \ 98 | x == SIGBUS ? "SIGBUS" : \ 99 | x == SIGUSR1 ? "SIGUSR1" : "UNKNOWN" 100 | 101 | 102 | /* protos */ 103 | 104 | extern void fatal(char *file, const char *function, int line, const char *fmt, ...); 105 | extern void logthis(char *file, const char *function, int line, int ifverbose,int h, int n, const char *fmt, ...); 106 | extern void logmem(u_int8_t *p, u_int32_t len, u_int32_t cols, int format, char *lh); 107 | extern char *str_char(unsigned char c); 108 | extern void enable_verbose(); 109 | extern void disable_verbose(); 110 | extern void open_logfile(char *pathname); 111 | extern void init_sighandlers(); 112 | extern void sig_lock(); 113 | extern void sig_unlock(); 114 | extern void close_logfile(); 115 | extern char *strtime(time_t t); 116 | extern void drop_privs(char *user, char *group); 117 | extern void daemonize(); 118 | extern int exists(char *pathname); 119 | extern void enable_syslog(); 120 | extern void disable_syslog(); 121 | extern void enable_stdout(); 122 | extern void disable_stdout(); 123 | extern char *get_next_name(char *directory, char *prefix, char *suffix, int *i); 124 | extern int isdirectory(char *pathname); 125 | extern char *trim(char *str); 126 | extern int parse_token(char *data, u_int32_t datalen, char *delims, char *found); 127 | extern int parse_line(char *data, u_int32_t datalen); 128 | extern int mystrnstr(char *str1, char *str2, int str1len); 129 | 130 | #endif 131 | 132 | 133 | /* eof */ 134 | 135 | -------------------------------------------------------------------------------- /src/rtp_pt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * rtp_pt.h by xenion -- 2008-05-05 -- v.4464c61a1c3fe5803ccbaa426c87a448 3 | * 4 | * Copyright (c) 2007-2008 Dallachiesa Michele 5 | * 6 | * 7 | * This program is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU General Public License 9 | * as published by the Free Software Foundation; either version 2 10 | * of the License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | * 21 | */ 22 | 23 | /* 24 | * RTP Payload types, from Wireshark sources. 25 | * Table B.2 / H.225.0 26 | * Also RFC 1890, and 27 | * http://www.iana.org/assignments/rtp-parameters 28 | */ 29 | 30 | 31 | typedef struct 32 | { 33 | int type; 34 | char *str; 35 | } 36 | value_string; 37 | 38 | 39 | #define PT_PCMU 0 /* RFC 1890 */ 40 | #define PT_1016 1 /* RFC 1890 */ 41 | #define PT_G721 2 /* RFC 1890 */ 42 | #define PT_GSM 3 /* RFC 1890 */ 43 | #define PT_G723 4 /* From Vineet Kumar of Intel; see the Web page */ 44 | #define PT_DVI4_8000 5 /* RFC 1890 */ 45 | #define PT_DVI4_16000 6 /* RFC 1890 */ 46 | #define PT_LPC 7 /* RFC 1890 */ 47 | #define PT_PCMA 8 /* RFC 1890 */ 48 | #define PT_G722 9 /* RFC 1890 */ 49 | #define PT_L16_STEREO 10 /* RFC 1890 */ 50 | #define PT_L16_MONO 11 /* RFC 1890 */ 51 | #define PT_QCELP 12 /* Qualcomm Code Excited Linear Predictive coding? */ 52 | #define PT_CN 13 /* RFC 3389 */ 53 | #define PT_MPA 14 /* RFC 1890, RFC 2250 */ 54 | #define PT_G728 15 /* RFC 1890 */ 55 | #define PT_DVI4_11025 16 /* from Joseph Di Pol of Sun; see the Web page */ 56 | #define PT_DVI4_22050 17 /* from Joseph Di Pol of Sun; see the Web page */ 57 | #define PT_G729 18 58 | #define PT_CN_OLD 19 /* Payload type reserved (old version Comfort Noise) */ 59 | #define PT_CELB 25 /* RFC 2029 */ 60 | #define PT_JPEG 26 /* RFC 2435 */ 61 | #define PT_NV 28 /* RFC 1890 */ 62 | #define PT_H261 31 /* RFC 2032 */ 63 | #define PT_MPV 32 /* RFC 2250 */ 64 | #define PT_MP2T 33 /* RFC 2250 */ 65 | #define PT_H263 34 /* from Chunrong Zhu of Intel; see the Web page */ 66 | 67 | 68 | const value_string rtp_payload_type_vals[] = 69 | { 70 | { 71 | PT_PCMU, "ITU-T G.711 PCMU" 72 | }, 73 | { PT_1016, "USA Federal Standard FS-1016" }, 74 | { PT_G721, "ITU-T G.721" }, 75 | { PT_GSM, "GSM 06.10" }, 76 | { PT_G723, "ITU-T G.723" }, 77 | { PT_DVI4_8000, "DVI4 8000 samples/s" }, 78 | { PT_DVI4_16000, "DVI4 16000 samples/s" }, 79 | { PT_LPC, "Experimental linear predictive encoding from Xerox PARC" }, 80 | { PT_PCMA, "ITU-T G.711 PCMA" }, 81 | { PT_G722, "ITU-T G.722" }, 82 | { PT_L16_STEREO, "16-bit uncompressed audio, stereo" }, 83 | { PT_L16_MONO, "16-bit uncompressed audio, monaural" }, 84 | { PT_QCELP, "Qualcomm Code Excited Linear Predictive coding" }, 85 | { PT_CN, "Comfort noise" }, 86 | { PT_MPA, "MPEG-I/II Audio"}, 87 | { PT_G728, "ITU-T G.728" }, 88 | { PT_DVI4_11025, "DVI4 11025 samples/s" }, 89 | { PT_DVI4_22050, "DVI4 22050 samples/s" }, 90 | { PT_G729, "ITU-T G.729" }, 91 | { PT_CN_OLD, "Comfort noise (old)" }, 92 | { PT_CELB, "Sun CellB video encoding" }, 93 | { PT_JPEG, "JPEG-compressed video" }, 94 | { PT_NV, "'nv' program" }, 95 | { PT_H261, "ITU-T H.261" }, 96 | { PT_MPV, "MPEG-I/II Video"}, 97 | { PT_MP2T, "MPEG-II transport streams"}, 98 | { PT_H263, "ITU-T H.263" }, 99 | { 0, NULL }, 100 | }; 101 | 102 | 103 | const value_string rtp_payload_type_short_vals[] = 104 | { 105 | { 106 | PT_PCMU, "g711U" 107 | }, 108 | { PT_1016, "fs-1016" }, 109 | { PT_G721, "g721" }, 110 | { PT_GSM, "GSM" }, 111 | { PT_G723, "g723" }, 112 | { PT_DVI4_8000, "DVI4 8k" }, 113 | { PT_DVI4_16000, "DVI4 16k" }, 114 | { PT_LPC, "Exp. from Xerox PARC" }, 115 | { PT_PCMA, "g711A" }, 116 | { PT_G722, "g722" }, 117 | { PT_L16_STEREO, "16-bit audio, stereo" }, 118 | { PT_L16_MONO, "16-bit audio, monaural" }, 119 | { PT_QCELP, "Qualcomm" }, 120 | { PT_CN, "CN" }, 121 | { PT_MPA, "MPEG-I/II Audio"}, 122 | { PT_G728, "g728" }, 123 | { PT_DVI4_11025, "DVI4 11k" }, 124 | { PT_DVI4_22050, "DVI4 22k" }, 125 | { PT_G729, "g729" }, 126 | { PT_CN_OLD, "CN(old)" }, 127 | { PT_CELB, "CellB" }, 128 | { PT_JPEG, "JPEG" }, 129 | { PT_NV, "NV" }, 130 | { PT_H261, "h261" }, 131 | { PT_MPV, "MPEG-I/II Video"}, 132 | { PT_MP2T, "MPEG-II streams"}, 133 | { PT_H263, "h263" }, 134 | { 0, NULL }, 135 | }; 136 | 137 | /* eof */ 138 | 139 | -------------------------------------------------------------------------------- /src/rtp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * rtp.h by xenion -- 2008-05-05 -- v.241ba0fea6fe7267cfbb3639b1b3ee3d 3 | * 4 | * Copyright (c) 2007-2008 Dallachiesa Michele 5 | * 6 | * This program is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU General Public License 8 | * as published by the Free Software Foundation; either version 2 9 | * of the License, or (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 | * 20 | */ 21 | 22 | // modified in order to use the O.S. bit order. 23 | 24 | /* 25 | * rtp.h -- RTP header file 26 | * RTP draft: November 1994 version 27 | * 28 | * $Id: rtp.h,v 1.3 1995/08/17 13:54:58 hgs Exp $ 29 | */ 30 | 31 | #ifndef RTP_H 32 | #define RTP_H 33 | 34 | #include // defines byte order for this machine. 35 | #include "rtp_pt.h" 36 | 37 | #define RTP_SEQ_MOD (1<<16) 38 | #define RTP_TS_MOD (0xffffffff) 39 | /* 40 | * Current type value. 41 | */ 42 | #define RTP_VERSION 2 43 | 44 | #define RTP_MAX_SDES 256 /* maximum text length for SDES */ 45 | 46 | typedef enum 47 | { 48 | RTCP_SR = 200, 49 | RTCP_RR = 201, 50 | RTCP_SDES = 202, 51 | RTCP_BYE = 203, 52 | RTCP_APP = 204 53 | } rtcp_type_t; 54 | 55 | typedef enum 56 | { 57 | RTCP_SDES_END = 0, 58 | RTCP_SDES_CNAME = 1, 59 | RTCP_SDES_NAME = 2, 60 | RTCP_SDES_EMAIL = 3, 61 | RTCP_SDES_PHONE = 4, 62 | RTCP_SDES_LOC = 5, 63 | RTCP_SDES_TOOL = 6, 64 | RTCP_SDES_NOTE = 7, 65 | RTCP_SDES_PRIV = 8, 66 | RTCP_SDES_IMG = 9, 67 | RTCP_SDES_DOOR = 10, 68 | RTCP_SDES_SOURCE = 11 69 | } rtcp_sdes_type_t; 70 | 71 | 72 | typedef struct 73 | { 74 | #if __BYTE_ORDER == __LITTLE_ENDIAN 75 | u_int8_t cc: 76 | 4; /* CSRC count */ 77 | u_int8_t x: 78 | 1; /* header extension flag */ 79 | u_int8_t p: 80 | 1; /* padding flag */ 81 | u_int8_t v: 82 | 2; /* protocol version */ 83 | u_int8_t pt: 84 | 7; /* payload type */ 85 | u_int8_t m: 86 | 1; /* marker bit */ 87 | #elif __BYTE_ORDER == __BIG_ENDIAN 88 | u_int8_t v: 89 | 2; /* protocol version */ 90 | u_int8_t p: 91 | 1; /* padding flag */ 92 | u_int8_t x: 93 | 1; /* header extension flag */ 94 | u_int8_t cc: 95 | 4; /* CSRC count */ 96 | u_int8_t m: 97 | 1; /* marker bit */ 98 | u_int8_t pt: 99 | 7; /* payload type */ 100 | #else 101 | # error "Please fix " 102 | #endif 103 | 104 | u_int16_t seq; /* sequence number */ 105 | u_int32_t ts; /* timestamp */ 106 | u_int32_t ssrc; /* synchronization source */ 107 | u_int32_t csrc[0]; /* optional CSRC list */ 108 | } 109 | rtp_hdr_t; 110 | 111 | typedef struct 112 | { 113 | u_int16_t profdef; 114 | u_int16_t length; // length of extension in 32bits, this header exluded. 115 | } 116 | rtp_extension_hdr_t; 117 | 118 | typedef struct 119 | { 120 | unsigned int version: 121 | 2; /* protocol version */ 122 | unsigned int p: 123 | 1; /* padding flag */ 124 | unsigned int count: 125 | 5; /* varies by payload type */ 126 | unsigned int pt: 127 | 8; /* payload type */ 128 | u_int16_t length; /* packet length in words, without this word */ 129 | } 130 | rtcp_common_t; 131 | 132 | /* reception report */ 133 | typedef struct 134 | { 135 | u_int32_t ssrc; /* data source being reported */ 136 | unsigned int fraction: 137 | 8; /* fraction lost since last SR/RR */ 138 | int lost: 139 | 24; /* cumulative number of packets lost (signed!) */ 140 | u_int32_t last_seq; /* extended last sequence number received */ 141 | u_int32_t jitter; /* interarrival jitter */ 142 | u_int32_t lsr; /* last SR packet from this source */ 143 | u_int32_t dlsr; /* delay since last SR packet */ 144 | } 145 | rtcp_rr_t; 146 | 147 | typedef struct 148 | { 149 | u_int8_t type; /* type of SDES item (rtcp_sdes_type_t) */ 150 | u_int8_t length; /* length of SDES item (in octets) */ 151 | char data[1]; /* text, not zero-terminated */ 152 | } 153 | rtcp_sdes_item_t; 154 | 155 | /* one RTCP packet */ 156 | typedef struct 157 | { 158 | rtcp_common_t common; /* common header */ 159 | union 160 | { 161 | /* sender report (SR) */ 162 | struct 163 | { 164 | u_int32_t ssrc; /* source this RTCP packet refers to */ 165 | u_int32_t ntp_sec; /* NTP timestamp */ 166 | u_int32_t ntp_frac; 167 | u_int32_t rtp_ts; /* RTP timestamp */ 168 | u_int32_t psent; /* packets sent */ 169 | u_int32_t osent; /* octets sent */ 170 | /* variable-length list */ 171 | rtcp_rr_t rr[1]; 172 | } 173 | sr; 174 | 175 | /* reception report (RR) */ 176 | struct 177 | { 178 | u_int32_t ssrc; /* source this generating this report */ 179 | /* variable-length list */ 180 | rtcp_rr_t rr[1]; 181 | } 182 | rr; 183 | 184 | /* BYE */ 185 | struct 186 | { 187 | u_int32_t src[1]; /* list of sources */ 188 | /* can't express trailing text */ 189 | } 190 | bye; 191 | 192 | /* source description (SDES) */ 193 | struct rtcp_sdes_t 194 | { 195 | u_int32_t src; /* first SSRC/CSRC */ 196 | rtcp_sdes_item_t item[1]; /* list of SDES items */ 197 | } 198 | sdes; 199 | } r; 200 | } 201 | rtcp_t; 202 | 203 | #endif 204 | -------------------------------------------------------------------------------- /src/common.c: -------------------------------------------------------------------------------- 1 | /* 2 | * common.c by xenion -- 2008-05-05 -- v.fdb23b830c7d63aa1208dfdad3ccf845 3 | * 4 | * Copyright (c) 2007-2008 Dallachiesa Michele 5 | * 6 | * 7 | * This program is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU General Public License 9 | * as published by the Free Software Foundation; either version 2 10 | * of the License, or (at your option) any later version. 11 | * 12 | * This program is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program; if not, write to the Free Software 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 | * 21 | */ 22 | 23 | 24 | #define _GNU_SOURCE 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include "common.h" 41 | 42 | 43 | /* globals */ 44 | 45 | static int verbose_enabled = 0; 46 | static FILE *f = NULL; 47 | static int syslog_enabled = 0; 48 | static int stdout_enabled = 0; 49 | static char line_buffer[LINE_BUFFER_MAX] = {0, }; 50 | 51 | 52 | /* extern */ 53 | 54 | extern void cleanup(); 55 | 56 | 57 | /* protos */ 58 | 59 | void sigdie(int signo); 60 | 61 | 62 | /*******************************************/ 63 | 64 | 65 | void enable_verbose() 66 | { 67 | verbose_enabled = 1; 68 | } 69 | 70 | 71 | void disable_verbose() 72 | { 73 | verbose_enabled = 0; 74 | } 75 | 76 | 77 | void enable_syslog() 78 | { 79 | syslog_enabled = 1; 80 | } 81 | 82 | 83 | void disable_syslog() 84 | { 85 | syslog_enabled = 0; 86 | } 87 | 88 | 89 | void enable_stdout() 90 | { 91 | stdout_enabled = 1; 92 | } 93 | 94 | 95 | void disable_stdout() 96 | { 97 | stdout_enabled = 0; 98 | } 99 | 100 | 101 | void open_logfile(char *pathname) 102 | { 103 | if (f) 104 | SAFE_FCLOSE(f); 105 | 106 | if ((f = fopen(pathname, "a")) == NULL) 107 | FATAL("fopen(): %s", strerror(errno)); 108 | } 109 | 110 | 111 | void close_logfile() 112 | { 113 | if (f != NULL && f != stdout && f != stderr) 114 | SAFE_FCLOSE(f); 115 | } 116 | 117 | 118 | void 119 | logthis(char *file, const char *function, int line, int ifverbose,int h, int n, const char *fmt, ...) 120 | { 121 | u_int32_t len; 122 | va_list ap; 123 | 124 | 125 | // The program must first execute the macro va_start within the body 126 | // of the function to initialize an object with context information. 127 | 128 | va_start(ap, fmt); 129 | 130 | if (ifverbose && verbose_enabled == 0) 131 | return; 132 | 133 | if (f == NULL) // stdout by default... 134 | f = stdout; 135 | if (f == stdout) // prevent double logging to stdout... 136 | disable_stdout(); 137 | 138 | if (h) 139 | { 140 | line_buffer[0] = '\0'; 141 | if (verbose_enabled) 142 | snprintf(line_buffer,LINE_BUFFER_MAX,"%s %s:%d:",strtime(time(NULL)),function, line); 143 | } 144 | 145 | len = strlen(line_buffer); 146 | 147 | vsnprintf(line_buffer+len,LINE_BUFFER_MAX-len,fmt, ap); 148 | 149 | len = strlen(line_buffer); 150 | 151 | if (len >= LINE_BUFFER_MAX-1) 152 | { 153 | // we reached the end-1... probably the string was truncated. we keep 1 byte for trailing '\n', inserted in if(n) .... 154 | line_buffer[0] = '\0'; 155 | FATAL("line buffer full"); 156 | } 157 | 158 | 159 | if (n) 160 | { 161 | 162 | line_buffer[len] = '\n'; 163 | line_buffer[len+1] = 0; 164 | 165 | fprintf(f, "%s", line_buffer); 166 | fflush(f); 167 | 168 | if (syslog_enabled) 169 | syslog(LOG_DAEMON|LOG_INFO, line_buffer); 170 | 171 | if (stdout_enabled) 172 | printf("%s", line_buffer); 173 | 174 | line_buffer[0] = 0; 175 | } 176 | 177 | va_end(ap); 178 | } 179 | 180 | 181 | void 182 | fatal(char *file, const char *function, int line, const char *fmt, ...) 183 | { 184 | va_list ap; 185 | 186 | 187 | // The program must first execute the macro va_start within the body 188 | // of the function to initialize an object with context information. 189 | 190 | va_start(ap, fmt); 191 | 192 | if (f == NULL) 193 | f = stdout; 194 | if (f == stdout) 195 | disable_stdout(); 196 | disable_verbose(); 197 | 198 | snprintf(line_buffer,LINE_BUFFER_MAX,"%s Fatal error at %s:%d:%s: ",strtime(time(NULL)),file, line, function); 199 | vsnprintf(line_buffer+strlen(line_buffer),LINE_BUFFER_MAX-strlen(line_buffer),fmt, ap); 200 | snprintf(line_buffer+strlen(line_buffer),LINE_BUFFER_MAX-strlen(line_buffer),"; exit forced."); 201 | 202 | if (strlen(line_buffer) >= LINE_BUFFER_MAX-1) 203 | { 204 | FATAL("line buffer full"); 205 | } 206 | 207 | fprintf(f,"--\n%s\n--\n", line_buffer); 208 | 209 | if (syslog_enabled) 210 | syslog(LOG_DAEMON | LOG_ERR,"%s", line_buffer); 211 | 212 | if (stdout_enabled) 213 | printf("--\n%s\n--\n", line_buffer); 214 | 215 | va_end(ap); 216 | 217 | sig_unlock(); // if lock, unlock. else, nothing changes. 218 | sigdie(-1); 219 | } 220 | 221 | 222 | void 223 | sigdie(int signo) 224 | { 225 | static int loop = 0; 226 | 227 | 228 | // if loop==1 happens, there's an infinite loop (and a bug somewhere ...). prevent it exiting. better than nothing... 229 | if (loop == 0) 230 | loop = 1; 231 | else 232 | exit(1); 233 | 234 | // if signo == -1, it's a direct call from function fatal: less output 235 | // messages looks better. 236 | 237 | if (signo != -1) 238 | { 239 | LOG(1,1,"--"); 240 | LOG(1,1,"Caught %s signal (%d), cleaning up...", SIG_NAME(signo), signo); 241 | LOG(1,1,"--"); 242 | } 243 | 244 | cleanup(); 245 | 246 | LOG(1,1,""); 247 | // questa deve essere l'ultima cosa prima della exit. 248 | disable_syslog(); 249 | close_logfile(); 250 | disable_stdout(); 251 | 252 | exit(signo == SIGTERM ? 0 : 1); // 0 == ok, 1 == err 253 | } 254 | 255 | 256 | void init_sighandlers() 257 | { 258 | signal(SIGSEGV, sigdie); 259 | signal(SIGTERM, sigdie); 260 | signal(SIGINT, sigdie); 261 | signal(SIGUSR1, sigdie); 262 | } 263 | 264 | 265 | void sig_lock() 266 | { 267 | signal(SIGTERM, SIG_IGN); 268 | signal(SIGINT, SIG_IGN); 269 | signal(SIGUSR1, SIG_IGN); 270 | } 271 | 272 | 273 | void sig_unlock() 274 | { 275 | signal(SIGTERM, sigdie); 276 | signal(SIGINT, sigdie); 277 | signal(SIGUSR1, sigdie); 278 | } 279 | 280 | 281 | char * 282 | strtime(time_t t) 283 | { 284 | struct tm *mytm; 285 | static char s[20]; 286 | 287 | 288 | mytm = localtime(&t); 289 | strftime(s, 20, "%d/%m/%Y#%H:%M:%S", mytm); 290 | return s; 291 | } 292 | 293 | 294 | void drop_privs(char *user, char *group) 295 | { 296 | struct passwd *p; 297 | struct group *g; 298 | int uid, gid; 299 | 300 | 301 | if (!user && !group) 302 | FATAL("(user == NULL && group == NULL)"); 303 | 304 | if ((p = getpwnam(user)) == NULL) 305 | FATAL("(getpwnam(...) == NULL): user not found"); 306 | 307 | uid = p->pw_uid; 308 | gid = p->pw_gid; 309 | 310 | if (group) 311 | { 312 | if ( (g = getgrnam(group)) == NULL) 313 | FATAL("(getgrnam(...) == NULL): group not found"); 314 | gid = g->gr_gid; 315 | } 316 | 317 | 318 | if (setgid(gid) == -1) 319 | FATAL("setgid(...): %s",strerror(errno)); 320 | 321 | if (setuid(uid) == -1) 322 | FATAL("setuid(...): %s",strerror(errno)); 323 | 324 | if (setegid(gid) == -1) 325 | FATAL("setegid(...): %s",strerror(errno)); 326 | 327 | if (seteuid(uid) == -1) 328 | FATAL("seteuid(...): %s",strerror(errno)); 329 | } 330 | 331 | 332 | void daemonize() 333 | { 334 | setsid(); 335 | if (fork()) 336 | exit(0); 337 | } 338 | 339 | 340 | int exists(char *pathname) 341 | { 342 | struct stat st; 343 | int z; 344 | 345 | 346 | z = stat(pathname, &st); 347 | 348 | if (z == 0) 349 | return 1; 350 | 351 | if (errno == ENOENT) 352 | return 0; 353 | 354 | return -1; // maybe exists, maybe perm problems.... 355 | } 356 | 357 | 358 | char *get_next_name(char *directory, char *prefix, char *suffix, int *i) 359 | { 360 | int count; 361 | static char pathname[PATH_MAX]; 362 | struct stat st; 363 | 364 | 365 | if (directory[0] != '\0' && // if directory specified... 366 | !isdirectory(directory)) 367 | FATAL("'%s' is not a valid directory, check pathname and permissions", directory); 368 | 369 | for (count = 0;;) 370 | { 371 | snprintf(pathname, PATH_MAX, "%s/%s%d%s", directory, prefix, count, suffix); 372 | if (stat(pathname, &st) == -1) 373 | { 374 | if (errno == ENOENT) 375 | break; 376 | else 377 | { 378 | if (i) 379 | *i = -1; 380 | return NULL; 381 | } 382 | } 383 | count++; 384 | } 385 | 386 | if (i) 387 | *i = count; 388 | 389 | return pathname; 390 | } 391 | 392 | 393 | int isdirectory(char *pathname) 394 | { 395 | struct stat st; 396 | 397 | if (stat(pathname, &st) != 0 || !S_ISDIR(st.st_mode)) 398 | return 0; 399 | else return 1; 400 | } 401 | 402 | 403 | int mystrnstr(char *str1, char *str2, int str1len) 404 | { 405 | int str2len, i; 406 | 407 | str2len = strlen(str2); 408 | 409 | if (str2len > str1len) 410 | return -1; 411 | 412 | for (i = 0; i <= str1len - str2len; i++) 413 | if (strncmp(str1+i, str2, str2len) == 0) 414 | return i; 415 | 416 | return -1; 417 | } 418 | 419 | 420 | char *trim(char *str) 421 | { 422 | char *p; 423 | int i; 424 | 425 | // LOG(1,1,"input is '%s'", str); 426 | 427 | for (p = str;*p != 0 && (*p == ' ' || *p == '\t');p++); 428 | if (*p == 0) // nothing to do! 429 | { 430 | // LOG(1,1,"output is '%s'", p); 431 | return p; 432 | } 433 | 434 | for (i = strlen(p);i >= 0 && (p[i] == 0 || p[i] == '\t' || p[i] == ' ');i--); 435 | if (i >= 0 && p[i] != 0) 436 | p[i+1] = 0; 437 | 438 | //LOG(1,1,"output is '%s'", p); 439 | 440 | 441 | return p; 442 | } 443 | 444 | 445 | int parse_token(char *data, u_int32_t datalen, char *delims, char *found) 446 | { 447 | int i,j,l; 448 | 449 | //LOG(1,1,"parse_token:"); 450 | //fwrite(data,1, datalen,stdout); 451 | //LOG(1,1,"-------------"); 452 | 453 | l = strlen(delims); 454 | for (i = 0; i < datalen; i++) 455 | { 456 | for (j = 0; j < l; j++) 457 | { 458 | if (data[i] == delims[j]) 459 | break; 460 | } 461 | if (data[i] == delims[j]) 462 | break; // propagate... 463 | } 464 | 465 | if (i == datalen) 466 | return -1; 467 | 468 | if (found) 469 | *found = data[i]; 470 | 471 | data[i] = 0; 472 | 473 | return i+1; 474 | } 475 | 476 | 477 | int parse_line(char *data, u_int32_t datalen) 478 | { 479 | int i; 480 | 481 | 482 | if (datalen <= 0) 483 | return -1; 484 | 485 | if ((i = parse_token(data, datalen, "\n",NULL)) == -1) 486 | return -1; 487 | 488 | if (i > 1 && data[i -2] == '\r') 489 | data[i-2] = 0; 490 | 491 | return i; 492 | } 493 | 494 | 495 | char *str_char(unsigned char c) 496 | { 497 | static char s[8]; 498 | 499 | 500 | if (c >= 32 && 126) 501 | { 502 | sprintf(s,"'%c' ", c); 503 | return s; 504 | } 505 | 506 | switch (c) 507 | { 508 | case '\0': 509 | return "'\\0'"; 510 | case '\r': 511 | return "'\\r'"; 512 | case '\n' : 513 | return "'\\n'"; 514 | default: 515 | sprintf(s,"?%.3d", c); 516 | return s; 517 | } 518 | 519 | } 520 | 521 | 522 | void 523 | logmem(u_int8_t *p, u_int32_t len, u_int32_t cols, int format, char *lh) 524 | { 525 | u_int32_t off,line,col; 526 | 527 | 528 | if (format == 1 && cols != 1) 529 | FATAL("with fmt == 1 you must use cols == 1"); 530 | 531 | if (!lh) 532 | lh = ""; 533 | 534 | LOG(1,0,"%s+ Dumping memory from %p for %d bytes, cols: %d, fmt: ",lh, p,len, cols); 535 | 536 | switch (format) 537 | { 538 | case 1: 539 | LOG(0,1,"hex bin dec chr"); 540 | break; 541 | case 2: 542 | LOG(0,1,"hex"); 543 | break; 544 | default: 545 | FATAL("undefined format: %d", format); 546 | } 547 | 548 | for (off=0,col=0,line=0;off 0) 553 | LOG(0,1,""); 554 | LOG(1,0,"%s| %p+%-8d",lh, p,off); 555 | } 556 | 557 | switch (format) 558 | { 559 | case 1: 560 | LOG(0,0,"0x%.2x %d%d%d%d%d%d%d%d %.3d %s",p[off],p[off] & 0x80 ? 1 : 0,p[off] & 0x40 ? 1 : 0,p[off] & 0x20 ? 1 : 0,p[off] & 0x10 ? 1 : 0,p[off] & 0x08 ? 1 : 0,p[off] & 0x04 ? 1 : 0,p[off] & 0x02 ? 1 : 0,p[off] & 0x01 ? 1 : 0, p[off], str_char(p[off])); 561 | break; 562 | case 2: 563 | LOG(0,0," %.2x",p[off]); 564 | break; 565 | default: 566 | FATAL("undefined format: %d", format); 567 | } 568 | 569 | if (++col >= cols) 570 | { 571 | col = 0; 572 | line++; 573 | } 574 | } 575 | 576 | LOG(0,1,""); 577 | LOG(1,1,"%s+ End", lh); 578 | } 579 | 580 | 581 | /* EOF*/ 582 | 583 | -------------------------------------------------------------------------------- /src/queue.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 1991, 1993 3 | * The Regents of the University of California. All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 4. Neither the name of the University nor the names of its contributors 14 | * may be used to endorse or promote products derived from this software 15 | * without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | * 29 | * @(#)queue.h 8.5 (Berkeley) 8/20/94 30 | * 31 | * Derived from FreeBSD src/sys/sys/queue.h:1.63. 32 | * $P4: //depot/projects/trustedbsd/openbsm/compat/queue.h#3 $ 33 | */ 34 | 35 | #ifndef _COMPAT_QUEUE_H_ 36 | #define _COMPAT_QUEUE_H_ 37 | 38 | #include 39 | 40 | /* 41 | * This file defines four types of data structures: singly-linked lists, 42 | * singly-linked tail queues, lists and tail queues. 43 | * 44 | * A singly-linked list is headed by a single forward pointer. The elements 45 | * are singly linked for minimum space and pointer manipulation overhead at 46 | * the expense of O(n) removal for arbitrary elements. New elements can be 47 | * added to the list after an existing element or at the head of the list. 48 | * Elements being removed from the head of the list should use the explicit 49 | * macro for this purpose for optimum efficiency. A singly-linked list may 50 | * only be traversed in the forward direction. Singly-linked lists are ideal 51 | * for applications with large datasets and few or no removals or for 52 | * implementing a LIFO queue. 53 | * 54 | * A singly-linked tail queue is headed by a pair of pointers, one to the 55 | * head of the list and the other to the tail of the list. The elements are 56 | * singly linked for minimum space and pointer manipulation overhead at the 57 | * expense of O(n) removal for arbitrary elements. New elements can be added 58 | * to the list after an existing element, at the head of the list, or at the 59 | * end of the list. Elements being removed from the head of the tail queue 60 | * should use the explicit macro for this purpose for optimum efficiency. 61 | * A singly-linked tail queue may only be traversed in the forward direction. 62 | * Singly-linked tail queues are ideal for applications with large datasets 63 | * and few or no removals or for implementing a FIFO queue. 64 | * 65 | * A list is headed by a single forward pointer (or an array of forward 66 | * pointers for a hash table header). The elements are doubly linked 67 | * so that an arbitrary element can be removed without a need to 68 | * traverse the list. New elements can be added to the list before 69 | * or after an existing element or at the head of the list. A list 70 | * may only be traversed in the forward direction. 71 | * 72 | * A tail queue is headed by a pair of pointers, one to the head of the 73 | * list and the other to the tail of the list. The elements are doubly 74 | * linked so that an arbitrary element can be removed without a need to 75 | * traverse the list. New elements can be added to the list before or 76 | * after an existing element, at the head of the list, or at the end of 77 | * the list. A tail queue may be traversed in either direction. 78 | * 79 | * For details on the use of these macros, see the queue(3) manual page. 80 | * 81 | * 82 | * SLIST LIST STAILQ TAILQ 83 | * _HEAD + + + + 84 | * _HEAD_INITIALIZER + + + + 85 | * _ENTRY + + + + 86 | * _INIT + + + + 87 | * _EMPTY + + + + 88 | * _FIRST + + + + 89 | * _NEXT + + + + 90 | * _PREV - - - + 91 | * _LAST - - + + 92 | * _FOREACH + + + + 93 | * _FOREACH_SAFE + + + + 94 | * _FOREACH_REVERSE - - - + 95 | * _FOREACH_REVERSE_SAFE - - - + 96 | * _INSERT_HEAD + + + + 97 | * _INSERT_BEFORE - + - + 98 | * _INSERT_AFTER + + + + 99 | * _INSERT_TAIL - - + + 100 | * _CONCAT - - + + 101 | * _REMOVE_HEAD + - + - 102 | * _REMOVE + + + + 103 | * 104 | */ 105 | #ifdef QUEUE_MACRO_DEBUG 106 | /* Store the last 2 places the queue element or head was altered */ 107 | struct qm_trace 108 | { 109 | char * lastfile; 110 | int lastline; 111 | char * prevfile; 112 | int prevline; 113 | }; 114 | 115 | #define TRACEBUF struct qm_trace trace; 116 | #define TRASHIT(x) do {(x) = (void *)-1;} while (0) 117 | 118 | #define QMD_TRACE_HEAD(head) do { \ 119 | (head)->trace.prevline = (head)->trace.lastline; \ 120 | (head)->trace.prevfile = (head)->trace.lastfile; \ 121 | (head)->trace.lastline = __LINE__; \ 122 | (head)->trace.lastfile = __FILE__; \ 123 | } while (0) 124 | 125 | #define QMD_TRACE_ELEM(elem) do { \ 126 | (elem)->trace.prevline = (elem)->trace.lastline; \ 127 | (elem)->trace.prevfile = (elem)->trace.lastfile; \ 128 | (elem)->trace.lastline = __LINE__; \ 129 | (elem)->trace.lastfile = __FILE__; \ 130 | } while (0) 131 | 132 | #else 133 | #define QMD_TRACE_ELEM(elem) 134 | #define QMD_TRACE_HEAD(head) 135 | #define TRACEBUF 136 | #define TRASHIT(x) 137 | #endif /* QUEUE_MACRO_DEBUG */ 138 | 139 | /* 140 | * Singly-linked List declarations. 141 | */ 142 | #define SLIST_HEAD(name, type) \ 143 | struct name { \ 144 | struct type *slh_first; /* first element */ \ 145 | } 146 | 147 | #define SLIST_HEAD_INITIALIZER(head) \ 148 | { NULL } 149 | 150 | #define SLIST_ENTRY(type) \ 151 | struct { \ 152 | struct type *sle_next; /* next element */ \ 153 | } 154 | 155 | /* 156 | * Singly-linked List functions. 157 | */ 158 | #define SLIST_EMPTY(head) ((head)->slh_first == NULL) 159 | 160 | #define SLIST_FIRST(head) ((head)->slh_first) 161 | 162 | #define SLIST_FOREACH(var, head, field) \ 163 | for ((var) = SLIST_FIRST((head)); \ 164 | (var); \ 165 | (var) = SLIST_NEXT((var), field)) 166 | 167 | #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ 168 | for ((var) = SLIST_FIRST((head)); \ 169 | (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ 170 | (var) = (tvar)) 171 | 172 | #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ 173 | for ((varp) = &SLIST_FIRST((head)); \ 174 | ((var) = *(varp)) != NULL; \ 175 | (varp) = &SLIST_NEXT((var), field)) 176 | 177 | #define SLIST_INIT(head) do { \ 178 | SLIST_FIRST((head)) = NULL; \ 179 | } while (0) 180 | 181 | #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ 182 | SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ 183 | SLIST_NEXT((slistelm), field) = (elm); \ 184 | } while (0) 185 | 186 | #define SLIST_INSERT_HEAD(head, elm, field) do { \ 187 | SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ 188 | SLIST_FIRST((head)) = (elm); \ 189 | } while (0) 190 | 191 | #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) 192 | 193 | #define SLIST_REMOVE(head, elm, type, field) do { \ 194 | if (SLIST_FIRST((head)) == (elm)) { \ 195 | SLIST_REMOVE_HEAD((head), field); \ 196 | } \ 197 | else { \ 198 | struct type *curelm = SLIST_FIRST((head)); \ 199 | while (SLIST_NEXT(curelm, field) != (elm)) \ 200 | curelm = SLIST_NEXT(curelm, field); \ 201 | SLIST_NEXT(curelm, field) = \ 202 | SLIST_NEXT(SLIST_NEXT(curelm, field), field); \ 203 | } \ 204 | TRASHIT((elm)->field.sle_next); \ 205 | } while (0) 206 | 207 | #define SLIST_REMOVE_HEAD(head, field) do { \ 208 | SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ 209 | } while (0) 210 | 211 | /* 212 | * Singly-linked Tail queue declarations. 213 | */ 214 | #define STAILQ_HEAD(name, type) \ 215 | struct name { \ 216 | struct type *stqh_first;/* first element */ \ 217 | struct type **stqh_last;/* addr of last next element */ \ 218 | } 219 | 220 | #define STAILQ_HEAD_INITIALIZER(head) \ 221 | { NULL, &(head).stqh_first } 222 | 223 | #define STAILQ_ENTRY(type) \ 224 | struct { \ 225 | struct type *stqe_next; /* next element */ \ 226 | } 227 | 228 | /* 229 | * Singly-linked Tail queue functions. 230 | */ 231 | #define STAILQ_CONCAT(head1, head2) do { \ 232 | if (!STAILQ_EMPTY((head2))) { \ 233 | *(head1)->stqh_last = (head2)->stqh_first; \ 234 | (head1)->stqh_last = (head2)->stqh_last; \ 235 | STAILQ_INIT((head2)); \ 236 | } \ 237 | } while (0) 238 | 239 | #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 240 | 241 | #define STAILQ_FIRST(head) ((head)->stqh_first) 242 | 243 | #define STAILQ_FOREACH(var, head, field) \ 244 | for((var) = STAILQ_FIRST((head)); \ 245 | (var); \ 246 | (var) = STAILQ_NEXT((var), field)) 247 | 248 | 249 | #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ 250 | for ((var) = STAILQ_FIRST((head)); \ 251 | (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 252 | (var) = (tvar)) 253 | 254 | #define STAILQ_INIT(head) do { \ 255 | STAILQ_FIRST((head)) = NULL; \ 256 | (head)->stqh_last = &STAILQ_FIRST((head)); \ 257 | } while (0) 258 | 259 | #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ 260 | if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ 261 | (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 262 | STAILQ_NEXT((tqelm), field) = (elm); \ 263 | } while (0) 264 | 265 | #define STAILQ_INSERT_HEAD(head, elm, field) do { \ 266 | if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ 267 | (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 268 | STAILQ_FIRST((head)) = (elm); \ 269 | } while (0) 270 | 271 | #define STAILQ_INSERT_TAIL(head, elm, field) do { \ 272 | STAILQ_NEXT((elm), field) = NULL; \ 273 | *(head)->stqh_last = (elm); \ 274 | (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 275 | } while (0) 276 | 277 | #define STAILQ_LAST(head, type, field) \ 278 | (STAILQ_EMPTY((head)) ? \ 279 | NULL : \ 280 | ((struct type *) \ 281 | ((char *)((head)->stqh_last) - __offsetof(struct type, field)))) 282 | 283 | #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 284 | 285 | #define STAILQ_REMOVE(head, elm, type, field) do { \ 286 | if (STAILQ_FIRST((head)) == (elm)) { \ 287 | STAILQ_REMOVE_HEAD((head), field); \ 288 | } \ 289 | else { \ 290 | struct type *curelm = STAILQ_FIRST((head)); \ 291 | while (STAILQ_NEXT(curelm, field) != (elm)) \ 292 | curelm = STAILQ_NEXT(curelm, field); \ 293 | if ((STAILQ_NEXT(curelm, field) = \ 294 | STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\ 295 | (head)->stqh_last = &STAILQ_NEXT((curelm), field);\ 296 | } \ 297 | TRASHIT((elm)->field.stqe_next); \ 298 | } while (0) 299 | 300 | #define STAILQ_REMOVE_HEAD(head, field) do { \ 301 | if ((STAILQ_FIRST((head)) = \ 302 | STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ 303 | (head)->stqh_last = &STAILQ_FIRST((head)); \ 304 | } while (0) 305 | 306 | #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \ 307 | if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \ 308 | (head)->stqh_last = &STAILQ_FIRST((head)); \ 309 | } while (0) 310 | 311 | /* 312 | * List declarations. 313 | */ 314 | #define LIST_HEAD(name, type) \ 315 | struct name { \ 316 | struct type *lh_first; /* first element */ \ 317 | } 318 | 319 | #define LIST_HEAD_INITIALIZER(head) \ 320 | { NULL } 321 | 322 | #define LIST_ENTRY(type) \ 323 | struct { \ 324 | struct type *le_next; /* next element */ \ 325 | struct type **le_prev; /* address of previous next element */ \ 326 | } 327 | 328 | /* 329 | * List functions. 330 | */ 331 | 332 | #if (defined(_KERNEL) && defined(INVARIANTS)) || defined(QUEUE_MACRO_DEBUG) 333 | #define QMD_LIST_CHECK_HEAD(head, field) do { \ 334 | if (LIST_FIRST((head)) != NULL && \ 335 | LIST_FIRST((head))->field.le_prev != \ 336 | &LIST_FIRST((head))) \ 337 | panic("Bad list head %p first->prev != head", (head)); \ 338 | } while (0) 339 | 340 | #define QMD_LIST_CHECK_NEXT(elm, field) do { \ 341 | if (LIST_NEXT((elm), field) != NULL && \ 342 | LIST_NEXT((elm), field)->field.le_prev != \ 343 | &((elm)->field.le_next)) \ 344 | panic("Bad link elm %p next->prev != elm", (elm)); \ 345 | } while (0) 346 | 347 | #define QMD_LIST_CHECK_PREV(elm, field) do { \ 348 | if (*(elm)->field.le_prev != (elm)) \ 349 | panic("Bad link elm %p prev->next != elm", (elm)); \ 350 | } while (0) 351 | #else 352 | #define QMD_LIST_CHECK_HEAD(head, field) 353 | #define QMD_LIST_CHECK_NEXT(elm, field) 354 | #define QMD_LIST_CHECK_PREV(elm, field) 355 | #endif /* (_KERNEL && INVARIANTS) || QUEUE_MACRO_DEBUG */ 356 | 357 | #define LIST_EMPTY(head) ((head)->lh_first == NULL) 358 | 359 | #define LIST_FIRST(head) ((head)->lh_first) 360 | 361 | #define LIST_FOREACH(var, head, field) \ 362 | for ((var) = LIST_FIRST((head)); \ 363 | (var); \ 364 | (var) = LIST_NEXT((var), field)) 365 | 366 | #define LIST_FOREACH_SAFE(var, head, field, tvar) \ 367 | for ((var) = LIST_FIRST((head)); \ 368 | (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 369 | (var) = (tvar)) 370 | 371 | #define LIST_INIT(head) do { \ 372 | LIST_FIRST((head)) = NULL; \ 373 | } while (0) 374 | 375 | #define LIST_INSERT_AFTER(listelm, elm, field) do { \ 376 | QMD_LIST_CHECK_NEXT(listelm, field); \ 377 | if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ 378 | LIST_NEXT((listelm), field)->field.le_prev = \ 379 | &LIST_NEXT((elm), field); \ 380 | LIST_NEXT((listelm), field) = (elm); \ 381 | (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ 382 | } while (0) 383 | 384 | #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 385 | QMD_LIST_CHECK_PREV(listelm, field); \ 386 | (elm)->field.le_prev = (listelm)->field.le_prev; \ 387 | LIST_NEXT((elm), field) = (listelm); \ 388 | *(listelm)->field.le_prev = (elm); \ 389 | (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ 390 | } while (0) 391 | 392 | #define LIST_INSERT_HEAD(head, elm, field) do { \ 393 | QMD_LIST_CHECK_HEAD((head), field); \ 394 | if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ 395 | LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ 396 | LIST_FIRST((head)) = (elm); \ 397 | (elm)->field.le_prev = &LIST_FIRST((head)); \ 398 | } while (0) 399 | 400 | #define LIST_NEXT(elm, field) ((elm)->field.le_next) 401 | 402 | #define LIST_REMOVE(elm, field) do { \ 403 | QMD_LIST_CHECK_NEXT(elm, field); \ 404 | QMD_LIST_CHECK_PREV(elm, field); \ 405 | if (LIST_NEXT((elm), field) != NULL) \ 406 | LIST_NEXT((elm), field)->field.le_prev = \ 407 | (elm)->field.le_prev; \ 408 | *(elm)->field.le_prev = LIST_NEXT((elm), field); \ 409 | TRASHIT((elm)->field.le_next); \ 410 | TRASHIT((elm)->field.le_prev); \ 411 | } while (0) 412 | 413 | /* 414 | * Tail queue declarations. 415 | */ 416 | #define TAILQ_HEAD(name, type) \ 417 | struct name { \ 418 | struct type *tqh_first; /* first element */ \ 419 | struct type **tqh_last; /* addr of last next element */ \ 420 | TRACEBUF \ 421 | } 422 | 423 | #define TAILQ_HEAD_INITIALIZER(head) \ 424 | { NULL, &(head).tqh_first } 425 | 426 | #define TAILQ_ENTRY(type) \ 427 | struct { \ 428 | struct type *tqe_next; /* next element */ \ 429 | struct type **tqe_prev; /* address of previous next element */ \ 430 | TRACEBUF \ 431 | } 432 | 433 | /* 434 | * Tail queue functions. 435 | */ 436 | #define TAILQ_CONCAT(head1, head2, field) do { \ 437 | if (!TAILQ_EMPTY(head2)) { \ 438 | *(head1)->tqh_last = (head2)->tqh_first; \ 439 | (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ 440 | (head1)->tqh_last = (head2)->tqh_last; \ 441 | TAILQ_INIT((head2)); \ 442 | QMD_TRACE_HEAD(head1); \ 443 | QMD_TRACE_HEAD(head2); \ 444 | } \ 445 | } while (0) 446 | 447 | #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 448 | 449 | #define TAILQ_FIRST(head) ((head)->tqh_first) 450 | 451 | #define TAILQ_FOREACH(var, head, field) \ 452 | for ((var) = TAILQ_FIRST((head)); \ 453 | (var); \ 454 | (var) = TAILQ_NEXT((var), field)) 455 | 456 | #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ 457 | for ((var) = TAILQ_FIRST((head)); \ 458 | (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ 459 | (var) = (tvar)) 460 | 461 | #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 462 | for ((var) = TAILQ_LAST((head), headname); \ 463 | (var); \ 464 | (var) = TAILQ_PREV((var), headname, field)) 465 | 466 | #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ 467 | for ((var) = TAILQ_LAST((head), headname); \ 468 | (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ 469 | (var) = (tvar)) 470 | 471 | #define TAILQ_INIT(head) do { \ 472 | TAILQ_FIRST((head)) = NULL; \ 473 | (head)->tqh_last = &TAILQ_FIRST((head)); \ 474 | QMD_TRACE_HEAD(head); \ 475 | } while (0) 476 | 477 | #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 478 | if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ 479 | TAILQ_NEXT((elm), field)->field.tqe_prev = \ 480 | &TAILQ_NEXT((elm), field); \ 481 | else { \ 482 | (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 483 | QMD_TRACE_HEAD(head); \ 484 | } \ 485 | TAILQ_NEXT((listelm), field) = (elm); \ 486 | (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ 487 | QMD_TRACE_ELEM(&(elm)->field); \ 488 | QMD_TRACE_ELEM(&listelm->field); \ 489 | } while (0) 490 | 491 | #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 492 | (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 493 | TAILQ_NEXT((elm), field) = (listelm); \ 494 | *(listelm)->field.tqe_prev = (elm); \ 495 | (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ 496 | QMD_TRACE_ELEM(&(elm)->field); \ 497 | QMD_TRACE_ELEM(&listelm->field); \ 498 | } while (0) 499 | 500 | #define TAILQ_INSERT_HEAD(head, elm, field) do { \ 501 | if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ 502 | TAILQ_FIRST((head))->field.tqe_prev = \ 503 | &TAILQ_NEXT((elm), field); \ 504 | else \ 505 | (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 506 | TAILQ_FIRST((head)) = (elm); \ 507 | (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ 508 | QMD_TRACE_HEAD(head); \ 509 | QMD_TRACE_ELEM(&(elm)->field); \ 510 | } while (0) 511 | 512 | #define TAILQ_INSERT_TAIL(head, elm, field) do { \ 513 | TAILQ_NEXT((elm), field) = NULL; \ 514 | (elm)->field.tqe_prev = (head)->tqh_last; \ 515 | *(head)->tqh_last = (elm); \ 516 | (head)->tqh_last = &TAILQ_NEXT((elm), field); \ 517 | QMD_TRACE_HEAD(head); \ 518 | QMD_TRACE_ELEM(&(elm)->field); \ 519 | } while (0) 520 | 521 | #define TAILQ_LAST(head, headname) \ 522 | (*(((struct headname *)((head)->tqh_last))->tqh_last)) 523 | 524 | #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 525 | 526 | #define TAILQ_PREV(elm, headname, field) \ 527 | (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 528 | 529 | #define TAILQ_REMOVE(head, elm, field) do { \ 530 | if ((TAILQ_NEXT((elm), field)) != NULL) \ 531 | TAILQ_NEXT((elm), field)->field.tqe_prev = \ 532 | (elm)->field.tqe_prev; \ 533 | else { \ 534 | (head)->tqh_last = (elm)->field.tqe_prev; \ 535 | QMD_TRACE_HEAD(head); \ 536 | } \ 537 | *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ 538 | TRASHIT((elm)->field.tqe_next); \ 539 | TRASHIT((elm)->field.tqe_prev); \ 540 | QMD_TRACE_ELEM(&(elm)->field); \ 541 | } while (0) 542 | 543 | #endif /* !_COMPAT_QUEUE_H_ */ 544 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | * 4 | * Copyright (c) 2007-2008 Dallachiesa Michele 5 | * Copyright (c) 2015 QXIP BV 6 | * 7 | * 8 | * This program is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License 10 | * as published by the Free Software Foundation; either version 2 11 | * of the License, or (at your option) any later version. 12 | * 13 | * This program is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public License 19 | * along with this program; if not, write to the Free Software 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 21 | * 22 | */ 23 | 24 | 25 | /* includes */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | // #include 32 | #include "queue.h" 33 | #include "rtp.h" 34 | #include "common.h" 35 | #include "net.h" 36 | #include "main.h" 37 | 38 | 39 | /* globals */ 40 | 41 | struct rtp_streams_list rtp_streams; 42 | pcap_t *mypcap = NULL; 43 | pcap_dumper_t *pdump_noise = NULL; 44 | struct timeval pcap_time; 45 | u_int32_t pktcount = 0; 46 | int ndxlog = -1; 47 | int running = 0; 48 | char errbuf[PCAP_ERRBUF_SIZE]; 49 | OPT o; 50 | 51 | 52 | /* protos */ 53 | 54 | int dissect_ieee80211(struct pcap_pkt *ppkt, u_int32_t pktoff, u_int32_t pktlen); 55 | int dissect_eth(struct pcap_pkt *ppkt, u_int32_t pktoff, u_int32_t pktlen); 56 | int dissect_ip(struct pcap_pkt *ppkt, u_int32_t pktoff, u_int32_t pktlen); 57 | int dissect_udp(struct pcap_pkt *ppkt,u_int32_t pktoff, u_int32_t pktlen, addrs_t addrs); 58 | int dissect_rtp(struct pcap_pkt *ppkt,u_int32_t pktoff, u_int32_t pktlen, addrs_t addrs); 59 | char *find_stream_rtp_pt(int pt, int short_vals); 60 | void loop(int dlltype, int dllength); 61 | int main(int argc, char **argv); 62 | void init_opt(int argc, char **argv); 63 | void help(); 64 | void cleanup(); 65 | int timeout(struct timeval *a, struct timeval *b, float t); 66 | struct rtp_stream_entry *rtp_search_stream(addrs_t addrs, rtp_hdr_t *rtphdr); 67 | int rtp_stream_ts_seq_check(struct rtp_stream_entry *rtp_stream); 68 | char *strtime(time_t t); 69 | void rtp_streams_init(); 70 | void rtp_stream_add_pkt(struct rtp_stream_entry *rtp_stream, pktrtp_t *pktrtp); 71 | void rtp_stream_flush(struct rtp_stream_entry *rtp_stream, int buf_timeout); 72 | void rtp_stream_open_files(struct rtp_stream_entry *rtp_stream); 73 | struct rtp_stream_entry *rtp_stream_add(pktrtp_t *pktrtp, addrs_t addrs); 74 | void rtp_stream_close(struct rtp_stream_entry *rtp_stream); 75 | void rtp_streams_close(); 76 | int rtp_pkt_handle(pktrtp_t *pktrtp, addrs_t addrs); 77 | void rtp_streams_timeout(); 78 | void rtp_stream_search_rev(struct rtp_stream_entry *rtp_stream); 79 | void sig_stats_handler(int signo); 80 | void print_stream_stat(struct rtp_stream_entry *rtp_stream); 81 | 82 | 83 | /* extern */ 84 | 85 | // here because in order to have this function defined, 86 | // I should add #define _XOPEN_SOURCE=600 that brokens 87 | // other things. 88 | float strtof(const char *nptr, char **endptr); 89 | 90 | 91 | /*******************************************/ 92 | 93 | 94 | int rtp_stream_ts_seq_check(struct rtp_stream_entry *rtp_stream) 95 | { 96 | struct rtpbuf_entry *rtpbuf; 97 | rtp_hdr_t *rtphdr; 98 | int64_t prev_ts,v; 99 | int32_t prev_seq; 100 | 101 | 102 | prev_seq = -1; 103 | prev_ts = -1; 104 | 105 | LIST_FOREACH(rtpbuf, &rtp_stream->pkts, l) 106 | { 107 | rtphdr = (rtp_hdr_t *)(rtpbuf->pktrtp.pcap.pkt + rtpbuf->pktrtp.hdroff); 108 | 109 | if (prev_seq != -1 && (u_int16_t)(prev_seq+1) != ntohs(rtphdr->seq)) 110 | return -1; 111 | prev_seq = ntohs(rtphdr->seq); 112 | 113 | v = (u_int32_t)ntohl(rtphdr->ts); 114 | if (v < rtp_stream->max_ts_seen) // maybe mod loop!! 115 | v += ((u_int32_t)(0-1))+1; 116 | //LOG(1,1,"ts: %llu",v); 117 | if (prev_ts > v) 118 | return -1; 119 | prev_ts = v; 120 | } 121 | 122 | return 0; 123 | } 124 | 125 | 126 | int dissect_ieee80211(struct pcap_pkt *ppkt, u_int32_t pktoff, u_int32_t pktlen) 127 | { 128 | struct ieee80211_frame *wh; 129 | int32_t len; 130 | 131 | 132 | if (pktlen < sizeof(struct ieee80211_frame)) 133 | { 134 | LOG(1,1," * warning: broken ieee 802.11 frame"); 135 | } 136 | 137 | wh = (struct ieee80211_frame *)(ppkt->pkt+pktoff); 138 | 139 | len = sizeof(struct ieee80211_frame); 140 | 141 | if ((wh->i_fc[0]&IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA) 142 | return 0; 143 | 144 | if ((wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS) 145 | len += IEEE80211_ADDR_LEN; 146 | if (IEEE80211_QOS_HAS_SEQ(wh)) 147 | len += sizeof(u_int16_t); 148 | 149 | len+=8; 150 | 151 | if (len > pktlen) // this packet is something not interesting 152 | { 153 | return 0; 154 | } 155 | 156 | if ( ntohs(*((int32_t *)&ppkt->pkt[len-2])) == ETHERTYPE_IP) 157 | return dissect_ip(ppkt,len,ppkt->hdr.caplen-len); 158 | 159 | return 0; 160 | } 161 | 162 | 163 | int dissect_eth(struct pcap_pkt *ppkt, u_int32_t pktoff, u_int32_t pktlen) 164 | { 165 | struct libnet_ethernet_hdr *eth; 166 | 167 | 168 | if (pktlen < sizeof(struct libnet_ethernet_hdr)) 169 | { 170 | LOG(1,1,"broken eth frame"); 171 | return 0; 172 | } 173 | 174 | eth = (struct libnet_ethernet_hdr *)ppkt->pkt+pktoff; 175 | 176 | if (ntohs(eth->ether_type) == ETHERTYPE_IP) 177 | return dissect_ip(ppkt,sizeof(struct libnet_ethernet_hdr),ppkt->hdr.caplen-sizeof(struct libnet_ethernet_hdr)); 178 | 179 | return 0; 180 | } 181 | 182 | 183 | int rtp_pkt_handle(pktrtp_t *pktrtp, addrs_t addrs) 184 | { 185 | struct rtp_stream_entry *rtp_stream; 186 | rtp_hdr_t *rtphdr; 187 | 188 | 189 | rtphdr = (rtp_hdr_t *)(pktrtp->pcap.pkt + pktrtp->hdroff); 190 | 191 | if (! (rtp_stream = rtp_search_stream(addrs, rtphdr))) 192 | rtp_stream = rtp_stream_add(pktrtp, addrs); 193 | 194 | 195 | rtp_stream_add_pkt(rtp_stream, pktrtp); 196 | rtp_stream_flush(rtp_stream,o.timeout_pkt); 197 | 198 | return 0; 199 | } 200 | 201 | 202 | int 203 | dissect_udp(struct pcap_pkt *ppkt, u_int32_t pktoff, u_int32_t pktlen, addrs_t addrs) 204 | { 205 | struct libnet_udp_hdr *pktudp; 206 | int32_t len; 207 | 208 | 209 | if (pktlen < sizeof(struct udphdr)) 210 | return 0; 211 | 212 | pktudp = (struct libnet_udp_hdr *) (ppkt->pkt + pktoff); 213 | 214 | len = pktlen - sizeof(struct udphdr); 215 | 216 | if (len != ntohs(pktudp->uh_ulen)-sizeof(struct udphdr)) 217 | return 0; 218 | 219 | addrs.type = ADDRS_TYPE_UDP; 220 | addrs.srcport = ntohs(pktudp->uh_sport); 221 | addrs.dstport = ntohs(pktudp->uh_dport); 222 | 223 | // LOG(1,0,"ip: %s:%d > ",INET_NTOA(addrs.srcaddr),addrs.srcport); 224 | // LOG(0,1,"%s:%d",INET_NTOA(addrs.dstaddr),addrs.dstport); 225 | 226 | return dissect_rtp(ppkt,pktoff+sizeof(struct libnet_udp_hdr),len,addrs); 227 | } 228 | 229 | 230 | void 231 | loop(int dlltype,int dllength) 232 | { 233 | struct pcap_pkt ppkt; 234 | 235 | 236 | ppkt.dllength = dllength; 237 | ppkt.dlltype = dlltype; 238 | 239 | for (;;) 240 | { 241 | ppkt.pkt = (u_int8_t *)pcap_next(mypcap, &ppkt.hdr); 242 | 243 | if (!ppkt.pkt) 244 | { 245 | if (o.iface) 246 | continue; 247 | 248 | if (o.rxfile) 249 | { 250 | LOG(1,1," * eof reached."); 251 | break; 252 | } 253 | } 254 | 255 | pktcount++; 256 | // LOG(1,1,"pktcount: %d", pktcount); 257 | 258 | if (ppkt.hdr.caplen != ppkt.hdr.len) 259 | { 260 | LOG(1,1,"warning: frame length is %d but caplength is %d (should not happen)", 261 | ppkt.hdr.len, ppkt.hdr.caplen); 262 | continue; 263 | } 264 | 265 | if (ppkt.hdr.caplen < dllength) 266 | { 267 | LOG(1,1,"warning: broken datalink frame"); 268 | continue; 269 | } 270 | 271 | 272 | pcap_time = ppkt.hdr.ts; 273 | 274 | switch (dlltype) 275 | { 276 | case AP_DLT_EN10MB: 277 | dissect_eth(&ppkt,0,ppkt.hdr.caplen); 278 | break; 279 | case AP_DLT_IEEE802_11: 280 | dissect_ieee80211(&ppkt,0,ppkt.hdr.caplen); 281 | break; 282 | default: 283 | dissect_ip(&ppkt,dllength,ppkt.hdr.caplen-dllength); //skip header and try... 284 | } 285 | 286 | rtp_streams_timeout(); 287 | } 288 | } 289 | 290 | 291 | void list_rtp_pt() 292 | { 293 | int i; 294 | 295 | 296 | LOG(1,1,""); 297 | LOG(1,1,"[known RTP payload types]"); 298 | LOG(1,1,""); 299 | for (i=0;rtp_payload_type_vals[i].str;i++) 300 | LOG(1,1,"%d = %s", rtp_payload_type_vals[i].type, rtp_payload_type_vals[i].str); 301 | 302 | LOG(1,1,""); 303 | } 304 | 305 | 306 | void cleanup() 307 | { 308 | rtp_streams_close(); 309 | 310 | 311 | if (running) 312 | raise(SIGUSR2); // show state... 313 | 314 | SAFE_PCAP_CLOSE(mypcap); 315 | SAFE_FREE(o.rxfile); 316 | SAFE_FREE(o.iface); 317 | SAFE_FREE(o.outdir); 318 | SAFE_FREE(o.user); 319 | SAFE_FREE(o.mypcap_filter); 320 | SAFE_PDCLOSE(pdump_noise); 321 | } 322 | 323 | 324 | void 325 | init_opt(int argc, char **argv) 326 | { 327 | int c; 328 | char pathname[PATH_MAX]; 329 | 330 | o.udp_hdr_even_dst_port = 0; 331 | o.udp_hdr_unpriv_ports = 0; 332 | o.mypcap_filter = NULL; 333 | o.rtp_hdr_pt = -1; 334 | o.fill_gaps = 0; 335 | o.verbose = 0; 336 | o.rxfile = NULL; 337 | o.iface = NULL; 338 | o.outdir = strdup(DEFAULT_OUTDIR); 339 | o.dllength = -1; 340 | o.timeout_pkt = PKT_TIMEOUT; 341 | o.timeout_pattern = RTP_STREAM_PATTERN_TIMEOUT; 342 | o.rtp_payload_length = -1; 343 | o.dump_noise = 0; 344 | o.pattern_pkts = RTP_STREAM_PATTERN_PKTS; 345 | o.user = NULL; 346 | o.daemonize = 0; 347 | o.promisc = 0; 348 | o.syslog = 0; 349 | o.stdout = 1; 350 | o.dump_raw = 1; 351 | o.dump_pcap = 1; 352 | o.dump_wav = 1; 353 | 354 | if (argc ==1) 355 | help(); 356 | 357 | opterr = 0; 358 | 359 | while ((c = getopt(argc, argv, "r:i:d:L:y:p:l:t:T:P:Z:unvgekDmFfhwW")) != EOF) 360 | switch (c) 361 | { 362 | 363 | case 'e': 364 | o.udp_hdr_even_dst_port = 1; 365 | break; 366 | 367 | case 'p': 368 | o.mypcap_filter = strdup(optarg); 369 | break; 370 | 371 | case 'y': 372 | o.rtp_hdr_pt= atoi(optarg); 373 | if (o.rtp_hdr_pt < 0) 374 | FATAL("rtp_hdr_pt < 0"); 375 | break; 376 | 377 | case 'g': 378 | o.fill_gaps = 1; 379 | break; 380 | 381 | case 'L': 382 | o.dllength = atoi(optarg); 383 | if (o.dllength <0) 384 | FATAL("dllength < 0"); 385 | break; 386 | 387 | case 'r': 388 | SAFE_FREE(o.rxfile); 389 | o.rxfile = strdup(optarg); 390 | break; 391 | 392 | case 'i': 393 | SAFE_FREE(o.iface); 394 | o.iface = strdup(optarg); 395 | break; 396 | 397 | case 'v': 398 | o.verbose = 1; 399 | break; 400 | 401 | case 'd': 402 | SAFE_FREE(o.outdir); 403 | o.outdir = strdup(optarg); 404 | break; 405 | 406 | case 'l': 407 | o.rtp_payload_length= atoi(optarg); 408 | if (o.rtp_payload_length < 0) 409 | FATAL("rtp_payload_length < 0"); 410 | break; 411 | 412 | case 't': 413 | /* from manpage of strtof: 414 | * Since 0 can legitimately be returned on both success and failure, the calling program 415 | * should set errno to 0 before the call, and then determine if an error occurred by 416 | * checking whether errno has a non-zero value after the call. 417 | */ 418 | errno = 0; 419 | o.timeout_pkt = strtof(optarg, NULL); 420 | if (errno != 0) 421 | FATAL("strtof(): %s", strerror(errno)); 422 | if (o.timeout_pkt < 0) 423 | FATAL("timeout_pkt < 0"); 424 | break; 425 | 426 | case 'T': 427 | errno = 0; 428 | o.timeout_pattern = strtof(optarg, NULL); 429 | if (errno != 0) 430 | FATAL("strtof(): %s", strerror(errno)); 431 | if (o.timeout_pattern < 0) 432 | FATAL("timeout_pattern < 0"); 433 | break; 434 | 435 | case 'u': 436 | o.udp_hdr_unpriv_ports = 1; 437 | break; 438 | 439 | case 'n': 440 | o.dump_noise =1; 441 | break; 442 | 443 | case 'P': 444 | o.pattern_pkts = atoi(optarg); 445 | if (o.pattern_pkts <=0) 446 | FATAL("pattern_pkts <= 0"); 447 | break; 448 | 449 | case 'k': 450 | list_rtp_pt(); 451 | exit(0); 452 | 453 | case 'Z': 454 | SAFE_FREE(o.user); 455 | o.user = strdup(optarg); 456 | break; 457 | 458 | case 'D': 459 | o.daemonize = 1; 460 | break; 461 | 462 | case 'm': 463 | o.promisc = 1; 464 | break; 465 | 466 | case 'F': 467 | o.syslog = 1; 468 | break; 469 | 470 | case 'f': 471 | o.stdout = 0; 472 | break; 473 | 474 | case 'h': 475 | help(); 476 | break; 477 | 478 | case 'w': 479 | o.dump_raw = 0; 480 | break; 481 | case 'W': 482 | o.dump_pcap = 0; 483 | break; 484 | case 'S': 485 | o.dump_wav = 0; 486 | break; 487 | 488 | default: 489 | FATAL("option '%c' invalid", optopt); 490 | } 491 | 492 | if (o.daemonize) 493 | o.stdout = 0; 494 | 495 | if (o.stdout) 496 | enable_stdout(); 497 | 498 | if (o.verbose) 499 | enable_verbose(); 500 | 501 | get_next_name(o.outdir, "rtp.",".txt",&ndxlog) ; 502 | if (ndxlog == -1) 503 | FATAL("get_next_name(...): %s", strerror(errno)); 504 | snprintf(pathname, PATH_MAX, "%s/rtp.%d.txt", o.outdir, ndxlog); 505 | open_logfile(pathname); 506 | 507 | if (o.syslog) 508 | enable_syslog(); 509 | 510 | if (o.iface && o.rxfile) 511 | FATAL("dup packet source: -r or -i"); 512 | 513 | if (!o.iface && !o.rxfile) 514 | FATAL("packet source required"); 515 | 516 | if (o.rxfile) 517 | if ( (mypcap = pcap_open_offline(o.rxfile, errbuf)) == NULL) 518 | FATAL("pcap_open_offline(): %s", errbuf); 519 | 520 | if (o.iface) 521 | if ((mypcap = pcap_open_live(o.iface, 65535, o.promisc, 0, errbuf)) == NULL) 522 | FATAL("pcap_open_live(): %s", errbuf); 523 | 524 | if (o.dllength == -1 && sizeof_datalink(mypcap) == -1) 525 | FATAL("sizeof_datalink == -1"); 526 | 527 | if (o.user) 528 | drop_privs(o.user, NULL); // group can be NULL, it's ok! 529 | 530 | if (o.dump_noise) 531 | { 532 | snprintf(pathname, PATH_MAX, "%s/rtp.%d.noise.pcap", o.outdir, ndxlog); 533 | if (!(pdump_noise = pcap_dump_open(mypcap, pathname))) 534 | FATAL("pcap_dump_open(): %s", pcap_geterr(mypcap)); 535 | } 536 | 537 | 538 | if (o.daemonize) 539 | daemonize(); 540 | 541 | 542 | LOG(1,1," + rtpbreak v%s running here!",VERSION); 543 | LOG(1,1," + pid: %d, date/time: %s",getpid(),strtime(time(NULL))); 544 | 545 | 546 | if (o.verbose) 547 | { 548 | LOG(1,0," + cmd: %s", argv[0]); 549 | for (c = 1; c < argc; c++) 550 | LOG(0,0," '%s'", argv[c]); 551 | LOG(0,1,""); 552 | } 553 | 554 | LOG(1,1," + Configuration"); 555 | 556 | LOG(1,1," + INPUT"); 557 | LOG(1,0," Packet source: "); 558 | 559 | if (o.rxfile) 560 | LOG(0,1,"rxfile '%s'", o.rxfile); 561 | else 562 | LOG(0,1,"iface '%s'", o.iface); 563 | 564 | LOG(1,0," Force datalink header length: "); 565 | if (o.dllength == -1) 566 | LOG(0,1,"disabled"); 567 | else 568 | LOG(0,1,"%d bytes", o.dllength); 569 | 570 | LOG(1,1," + OUTPUT"); 571 | 572 | LOG(1,1," Output directory: '%s'", o.outdir); 573 | LOG(1,1," RTP raw dumps: %s", o.dump_raw ? "enabled" : "disabled"); 574 | LOG(1,1," RTP pcap dumps: %s", o.dump_pcap ? "enabled" : "disabled"); 575 | 576 | if (o.dump_raw) 577 | LOG(1,1," Fill gaps: %s",o.fill_gaps ? "enabled" : "disabled"); 578 | 579 | LOG(1,0," Dump noise: "); 580 | if (o.dump_noise) 581 | LOG(0,1,"'%s/rtp.%d.noise.pcap'", o.outdir, ndxlog); 582 | else 583 | LOG(0,1,"disabled"); 584 | 585 | LOG(1,1," Logfile: '%s/rtp.%d.txt'", o.outdir, ndxlog); 586 | LOG(1,1," Logging to stdout: %s",o.stdout ? "enabled" : "disabled"); 587 | LOG(1,1," Logging to syslog: %s",o.syslog ? "enabled" : "disabled"); 588 | LOG(1,1," Be verbose: %s",o.verbose ? "enabled" : "disabled"); 589 | LOG(1,1," + SELECT"); 590 | LOG(1,1," Sniff packets in promisc mode: %s", o.promisc ? "enabled" : "disabled"); 591 | 592 | LOG(1,0," Add pcap filter: "); 593 | if (o.mypcap_filter) 594 | LOG(0,1,"'%s'", o.mypcap_filter); 595 | else 596 | LOG(0,1,"disabled"); 597 | 598 | LOG(1,1," Expecting even destination UDP port: %s",o.udp_hdr_even_dst_port ? "enabled" : "disabled"); 599 | 600 | LOG(1,1," Expecting unprivileged source/destination UDP ports: %s", o.udp_hdr_unpriv_ports ? "enabled" : "disabled"); 601 | 602 | LOG(1,0," Expecting RTP payload type: "); 603 | if (o.rtp_hdr_pt == -1) 604 | LOG(0,1,"any"); 605 | else 606 | LOG(0,1,"%d (%s)", o.rtp_hdr_pt, find_stream_rtp_pt(o.rtp_hdr_pt,0)); 607 | 608 | LOG(1,0," Expecting RTP payload length: "); 609 | if (o.rtp_payload_length == -1) 610 | LOG(0,1,"any"); 611 | else 612 | LOG(0,1,"%d bytes", o.rtp_payload_length); 613 | 614 | LOG(1,1," Packet timeout: %.2f seconds", o.timeout_pkt); 615 | LOG(1,1," Pattern timeout: %.2f seconds", o.timeout_pattern); 616 | LOG(1,1," Pattern packets: %d", o.pattern_pkts); 617 | 618 | LOG(1,1," + EXECUTION"); 619 | LOG(1,1," Running as user/group: %s/%s", getpwuid(getuid())->pw_name,getgrgid(getgid())->gr_name); 620 | LOG(1,1," Running daemonized: %s", o.daemonize ? "enabled" : "disabled"); 621 | 622 | 623 | } 624 | 625 | 626 | void rtp_streams_init() 627 | { 628 | rtp_streams.list.lh_first = NULL; 629 | rtp_streams.max_id = 0; 630 | rtp_streams.closed = 0; 631 | rtp_streams.nclosed = 0; 632 | rtp_streams.active = 0; 633 | rtp_streams.pktcount = 0; 634 | rtp_streams.pktcount_noise = 0; 635 | rtp_streams.pktcount_lost = 0; 636 | } 637 | 638 | 639 | int 640 | main(int argc, char **argv) 641 | { 642 | init_sighandlers(); 643 | signal(SIGUSR2, sig_stats_handler); 644 | init_opt(argc, argv); 645 | rtp_streams_init(); 646 | 647 | 648 | if (o.mypcap_filter) 649 | add_pcap_filter(mypcap,o.mypcap_filter); 650 | 651 | LOG(1,1," * You can dump stats sending me a SIGUSR2 signal"); 652 | 653 | LOG(1,1," * Reading packets..."); 654 | 655 | running = 1; 656 | 657 | loop(pcap_datalink(mypcap),o.dllength == -1 ? sizeof_datalink(mypcap) : o.dllength); 658 | 659 | raise(SIGTERM); 660 | return 0; // never reached 661 | } 662 | 663 | 664 | int dissect_rtp(struct pcap_pkt *ppkt,u_int32_t pktoff, u_int32_t pktlen, addrs_t addrs) 665 | { 666 | pktrtp_t pktrtp; // rtp packet 667 | int32_t len; 668 | int32_t off; 669 | rtp_hdr_t *rtphdr; 670 | 671 | 672 | // rtcp solitamente gira su porte non privilegiate >1024... 673 | if (o.udp_hdr_unpriv_ports) 674 | if (addrs.dstport < 1024 || addrs.srcport < 1024) 675 | return 0; 676 | 677 | // rtcp gira su porte dispari mentre rtp su porte pari... 678 | // skippiamo le dispari. 679 | 680 | if (o.udp_hdr_even_dst_port) 681 | if (addrs.dstport % 2 != 0) 682 | return 0; 683 | 684 | if (pktlen <= sizeof(rtp_hdr_t)) 685 | return 0; 686 | 687 | pktrtp.hdroff = pktoff; 688 | pktrtp.len = pktlen; 689 | 690 | 691 | rtphdr = (rtp_hdr_t *)(ppkt->pkt + pktoff); 692 | 693 | pktrtp.pcap = *ppkt; 694 | 695 | 696 | if (o.rtp_hdr_pt != -1) 697 | if (rtphdr->pt !=o.rtp_hdr_pt) 698 | return 0; 699 | 700 | if (rtphdr->v != 2) 701 | return 0; 702 | 703 | //VLOG(1,1,"pktcount: %d", pktcount); 704 | 705 | off = pktrtp.hdroff + sizeof(rtp_hdr_t); 706 | len = pktlen - sizeof(rtp_hdr_t); 707 | 708 | // se il flag per il padding e' 1 allora l'ultimo byte del padding 709 | // indica quanti byte di padding sono presenti. 710 | // (mai notati, ma non usando encryption e' normale). 711 | if (rtphdr->p) 712 | len-= ((u_int8_t *)rtphdr)[pktlen-1]; 713 | 714 | // seguono i CSRC, ciascuno di 4 byte. il loro numero e' pari a 715 | // pktrtp.cc (li ho notati solo in yahoo messenger). 716 | if (rtphdr->cc >0) 717 | { 718 | len -= 4 * rtphdr->cc; 719 | off += 4 * rtphdr->cc; 720 | } 721 | 722 | if (rtphdr->x) 723 | { 724 | rtp_extension_hdr_t *rtpext; 725 | // l'extension header e' di 4 byte. 726 | if (len < 4) 727 | return 0; 728 | 729 | // the extension, if present, is after the CSRC list. 730 | rtpext = (rtp_extension_hdr_t *)((u_int8_t *)rtphdr+off); 731 | off += sizeof(rtp_extension_hdr_t) + rtpext->length; 732 | len -= sizeof(rtp_extension_hdr_t) + rtpext->length; 733 | } 734 | 735 | if (len < 0) 736 | return 0; 737 | 738 | pktrtp.payload.off = off; 739 | pktrtp.payload.len = len; 740 | 741 | if ( o.rtp_payload_length != -1) 742 | if (pktrtp.payload.len != o.rtp_payload_length) 743 | return 0; 744 | 745 | rtp_pkt_handle(&pktrtp,addrs); 746 | 747 | return 0; 748 | } 749 | 750 | 751 | int dissect_ip(struct pcap_pkt *ppkt, u_int32_t pktoff, u_int32_t pktlen) 752 | { 753 | addrs_t addrs = { 0,0,0,0}; 754 | struct libnet_ipv4_hdr *pktip; 755 | int32_t len; 756 | 757 | 758 | if (pktlen < sizeof(struct libnet_ipv4_hdr)) 759 | return 0; 760 | 761 | pktip = (struct libnet_ipv4_hdr *) (ppkt->pkt + pktoff); 762 | 763 | len = ntohs(pktip->ip_len) - (pktip->ip_hl << 2); 764 | 765 | // nota: potrebbe esserci il trailer eth 766 | 767 | if (len < 0 || len > pktlen) 768 | return 0; 769 | 770 | addrs.srcaddr = pktip->ip_src.s_addr; 771 | addrs.dstaddr = pktip->ip_dst.s_addr; 772 | 773 | if (pktip->ip_p == IPPROTO_UDP) 774 | return dissect_udp(ppkt,pktoff+(pktip->ip_hl << 2),len,addrs); 775 | 776 | return 0; 777 | } 778 | 779 | 780 | struct rtp_stream_entry *rtp_search_stream(addrs_t addrs, rtp_hdr_t *pktrtp) 781 | { 782 | struct rtp_stream_entry *rtp_stream; 783 | int64_t vmin, vmax, v; 784 | // non usiamo u_int16_t u_int32_t perche per calcolare vmin,vmax e' possibile uscire dal range, del seq e del ts. 785 | 786 | 787 | //VLOG(1,1,"searching stream..."); 788 | 789 | LIST_FOREACH(rtp_stream, &rtp_streams.list, l) 790 | { 791 | if (pktrtp->ssrc != rtp_stream->ssrc) 792 | continue; 793 | 794 | if (!(addrs.srcaddr == rtp_stream->addrs.srcaddr && 795 | addrs.dstaddr == rtp_stream->addrs.dstaddr && 796 | addrs.srcport == rtp_stream->addrs.srcport && 797 | addrs.dstport == rtp_stream->addrs.dstport)) 798 | continue; 799 | 800 | // controlliamo il sequence number: 801 | // consideriamo appartenenti a questo flusso 802 | // i sequence number compresi in una "finestra" 803 | // ampia RTP_SEQWINDOW, che ha come valore centrale 804 | // l'ultimo sequence number che abbiamo precedentemente 805 | // identificato come appartenente allo stream. 806 | 807 | v = ntohs(pktrtp->seq); 808 | 809 | if (rtp_stream->first_pkt.tv_sec != 0) // se none' il primo pkt... 810 | if (v < rtp_stream->max_seq_seen) // maybe mod loop!! 811 | v += ((u_int16_t)(0-1))+1; 812 | 813 | vmin = ((int64_t)rtp_stream->max_seq_seen) - RTP_SEQWINDOW / 2; 814 | vmax = ((int64_t)rtp_stream->max_seq_seen) + RTP_SEQWINDOW / 2; 815 | 816 | //VLOG(1,1,"seqcheck: vmin=%llu v=%llu vmax=%llu", vmin, v, vmax); 817 | if (v < vmin && v > vmax) 818 | { 819 | LOG(1,1,"seqcheck failed: vmin=%llu v=%llu vmax=%llu", vmin, v, vmax); 820 | continue; 821 | } 822 | 823 | v = ntohl(pktrtp->ts); 824 | 825 | if (rtp_stream->first_pkt.tv_sec != 0) // se none' il primo pkt... 826 | if (v < rtp_stream->max_ts_seen) // maybe mod loop!! 827 | v += ((u_int32_t)(0-1))+1; 828 | 829 | vmin = ((int64_t)rtp_stream->max_ts_seen) - RTP_TSWINDOW / 2; 830 | vmax = ((int64_t)rtp_stream->max_ts_seen) + RTP_TSWINDOW / 2; 831 | 832 | // LOG(1,1,"tscheck: vmin=%llu v=%llu vmax=%llu", vmin, v, vmax); 833 | if (v < vmin && v > vmax) 834 | { 835 | // LOG(1,1,"tscheck failed"); 836 | continue; 837 | } 838 | 839 | break; 840 | } 841 | 842 | /* 843 | if(rtp_stream) 844 | VLOG(1,1,"found!"); 845 | else 846 | VLOG(1,1,"not found!"); 847 | */ 848 | 849 | return rtp_stream; 850 | } 851 | 852 | 853 | void rtp_stream_add_pkt(struct rtp_stream_entry *rtp_stream, pktrtp_t *pktrtp) 854 | { 855 | struct rtpbuf_entry *rtpbuf,*after,*new, *before; 856 | rtp_hdr_t *rtphdr, *rtphdr2; 857 | 858 | 859 | before = NULL; 860 | after = NULL; 861 | rtpbuf = NULL; 862 | 863 | rtphdr = (rtp_hdr_t *)(pktrtp->pcap.pkt + pktrtp->hdroff); 864 | 865 | if (rtp_stream->pktcount_flhd == 0) 866 | rtp_stream->payload_type = rtphdr->pt; 867 | 868 | // 65535/2 per evitare possibili pacchetti duplicati ma gia' inseriti! 869 | // succede anche quando sipcodec sta girando. 870 | // rtp_stream->first_pkt.tv_sec != 0 ci assicura che non stiamo facendo 871 | // il check sul primo pkt... che risulta sempre positivo, se il seq parte da 0 872 | // come ad esempio in linphone. 873 | 874 | if (rtp_stream->first_pkt.tv_sec != 0 && ntohs(rtphdr->seq)+65535/2 < rtp_stream->max_seq_seen) 875 | { 876 | // mod loop!! forziamo il flushing del buffer... 877 | // e se il pattern none' gia' stato trovato, chiudiamo la sessione. 878 | // se era una sessione, verra' ricreata senza 879 | // cadere in questa situazione. cosi' ci evitiamo di 880 | // gestirla, che e' una palla e capita di rado. 881 | if (!rtp_stream->pattern_found) 882 | { 883 | rtp_stream_close(rtp_stream); 884 | return; 885 | } 886 | 887 | rtp_stream_flush(rtp_stream,0); 888 | 889 | if (rtp_stream->pkts.lh_first) 890 | FATAL("packets buffer not empty"); 891 | 892 | rtp_stream->max_ts_seen = ntohl(rtphdr->ts); 893 | rtp_stream->max_seq_seen = ntohs(rtphdr->seq); 894 | 895 | } 896 | else 897 | { 898 | 899 | if (rtp_stream->first_pkt.tv_sec != 0) 900 | if (ntohs(rtphdr->seq)<= rtp_stream->max_seq_seen) 901 | return; 902 | } 903 | 904 | // la lista contiene i pkt ordinati con seq in ordine crescente 905 | LIST_FOREACH(rtpbuf, &rtp_stream->pkts, l) 906 | { 907 | rtphdr2 = (rtp_hdr_t *)(rtpbuf->pktrtp.pcap.pkt + rtpbuf->pktrtp.hdroff); 908 | 909 | if (rtphdr->seq == rtphdr2->seq) 910 | { 911 | // LOG(1,1,"dup"); 912 | return; 913 | } 914 | if (ntohs(rtphdr->seq) < ntohs(rtphdr2->seq)) 915 | { 916 | LOG(1,1,"seq=%u insert before seq=%u", ntohs(rtphdr->seq),ntohs(rtphdr2->seq)); 917 | before = rtpbuf; 918 | // il nuovo pacchetto X ha un seqnum minore di quello 919 | // che stiamo osservando nella lista, Y. quindi X va 920 | // inserito prima di Y. 921 | LOG(1,1,"breaking"); 922 | break; 923 | } 924 | after = rtpbuf; 925 | } 926 | 927 | sig_lock(); // impediamo ctrl-c,... 928 | 929 | new = (struct rtpbuf_entry *)malloc(sizeof(struct rtpbuf_entry)); 930 | memcpy(&new->pktrtp, &pktrtp, sizeof(pktrtp_t)); 931 | new->pktrtp.pcap.hdr = pktrtp->pcap.hdr; 932 | new->pktrtp.pcap.pkt = (u_int8_t *)malloc(pktrtp->pcap.hdr.caplen); 933 | memcpy(new->pktrtp.pcap.pkt, pktrtp->pcap.pkt, pktrtp->pcap.hdr.caplen); 934 | new->pktrtp.len = pktrtp->len; 935 | new->pktrtp.hdroff = pktrtp->hdroff; 936 | new->pktrtp.payload.off = pktrtp->payload.off; 937 | new->pktrtp.payload.len = pktrtp->payload.len; 938 | 939 | if (rtp_stream->pkts.lh_first == NULL) 940 | { 941 | LIST_INSERT_HEAD(&rtp_stream->pkts, new,l); 942 | } 943 | else 944 | { 945 | if (before) 946 | { 947 | LIST_INSERT_BEFORE(before, new, l); 948 | } 949 | else 950 | { 951 | if (after) 952 | LIST_INSERT_AFTER(after, new, l); 953 | else FATAL("buffer not empty and before,after NULL"); 954 | } 955 | } 956 | 957 | if (rtp_stream->first_pkt.tv_sec == 0) 958 | rtp_stream->first_pkt = pktrtp->pcap.hdr.ts; 959 | rtp_stream->last_pkt = pktrtp->pcap.hdr.ts; 960 | 961 | rtp_stream->pktcount_inbuf++; 962 | 963 | if (ntohl(rtphdr->ts) == 0 && rtp_stream->max_ts_seen == ((u_int32_t)(0-1))) 964 | rtp_stream->max_ts_seen = 0; 965 | rtp_stream->max_ts_seen = MAX(rtp_stream->max_ts_seen, ntohl(rtphdr->ts)); 966 | 967 | if (ntohs(rtphdr->seq) == 0 && rtp_stream->max_seq_seen == ((u_int16_t)(0-1))) 968 | rtp_stream->max_seq_seen = 0; 969 | else 970 | rtp_stream->max_seq_seen = MAX(rtp_stream->max_seq_seen, ntohs(rtphdr->seq)); 971 | 972 | if (rtp_stream->last_payload_length != -1 && 973 | rtp_stream->last_payload_length != pktrtp->payload.len) 974 | rtp_stream->payload_length_fixed = 0; 975 | rtp_stream->last_payload_length = pktrtp->payload.len; 976 | 977 | sig_unlock(); 978 | 979 | // we can perform a timestamp check: 980 | // (pkt_before.ts <= pkt.ts <= pkt_after.ts) 981 | 982 | if (!rtp_stream->pattern_found) 983 | if (rtp_stream->pktcount_inbuf >= o.pattern_pkts) 984 | { 985 | 986 | // facciamo due controlli: verifichiamo che non 987 | // ci siano salti fra i seq e che: 988 | // (pkt_before.ts <= pkt.ts <= pkt_after.ts) 989 | 990 | // questi due controlli vengono fatti soltanto 991 | // se !rtp_stream->pattern_found 992 | 993 | if (rtp_stream_ts_seq_check(rtp_stream) == -1) 994 | { 995 | rtp_stream_close(rtp_stream); 996 | return; 997 | } 998 | 999 | rtp_streams.active++; 1000 | 1001 | rtp_stream->pattern_found=1; 1002 | 1003 | // "detected!..." nella flush, cosi' abbiamo anche il fid. 1004 | } 1005 | 1006 | } 1007 | 1008 | 1009 | void rtp_stream_flush(struct rtp_stream_entry *rtp_stream, int buf_timeout) 1010 | { 1011 | struct rtpbuf_entry *rtpbuf, *rtpbuf2; 1012 | rtp_hdr_t *rtphdr; 1013 | int i,j; 1014 | 1015 | 1016 | if (!rtp_stream->pattern_found) 1017 | { 1018 | 1019 | if (buf_timeout != 0) 1020 | return; 1021 | 1022 | // qui ci finiamo soltanto quando chiudiamo la sessione, un falso positivo. 1023 | 1024 | LIST_FOREACH_SAFE(rtpbuf, &rtp_stream->pkts, l, rtpbuf2) 1025 | { 1026 | 1027 | sig_lock(); 1028 | if (pdump_noise) 1029 | pcap_dump((u_char *)pdump_noise, &rtpbuf->pktrtp.pcap.hdr, rtpbuf->pktrtp.pcap.pkt); 1030 | 1031 | rtp_streams.pktcount_noise++; 1032 | 1033 | LIST_REMOVE(rtpbuf, l); 1034 | SAFE_FREE(rtpbuf->pktrtp.pcap.pkt); 1035 | SAFE_FREE(rtpbuf); 1036 | sig_unlock(); 1037 | 1038 | } 1039 | 1040 | return; 1041 | } 1042 | 1043 | // open log files for entry... 1044 | if (!rtp_stream->f) 1045 | { 1046 | 1047 | rtp_stream_open_files(rtp_stream); 1048 | 1049 | LOG(1,0," ! [rtp%d] detected: pt=%d(%s) ",rtp_stream->fid,rtp_stream->payload_type,find_stream_rtp_pt(rtp_stream->payload_type,1)); 1050 | 1051 | LOG(0,0,"%s:%d => ",INET_NTOA(rtp_stream->addrs.srcaddr),rtp_stream->addrs.srcport); 1052 | 1053 | LOG(0,1,"%s:%d",INET_NTOA(rtp_stream->addrs.dstaddr),rtp_stream->addrs.dstport); 1054 | 1055 | SAFE_FPRINTF(rtp_stream->f,"RTP stream id: rtp.%d.%d\n", ndxlog, rtp_stream->fid); 1056 | if (o.iface) 1057 | SAFE_FPRINTF(rtp_stream->f,"Packet source: iface '%s'\n", o.iface); 1058 | if (o.rxfile) 1059 | SAFE_FPRINTF(rtp_stream->f,"Packet source: rxfile '%s'\n", o.rxfile); 1060 | 1061 | SAFE_FPRINTF(rtp_stream->f,"First seen packet: %s (pcap time)\n", strtime(rtp_stream->first_pkt.tv_sec)); 1062 | SAFE_FPRINTF(rtp_stream->f, "Stream peers: %s:%d => ",INET_NTOA(rtp_stream->addrs.srcaddr),rtp_stream->addrs.srcport); 1063 | SAFE_FPRINTF(rtp_stream->f,"%s:%d\n",INET_NTOA(rtp_stream->addrs.dstaddr),rtp_stream->addrs.dstport); 1064 | SAFE_FPRINTF(rtp_stream->f,"RTP ssrc: %u\n", ntohl(rtp_stream->ssrc)); 1065 | SAFE_FPRINTF(rtp_stream->f,"RTP payload type: %d (%s)\n", rtp_stream->payload_type, find_stream_rtp_pt(rtp_stream->payload_type,0)); 1066 | 1067 | rtp_stream_search_rev(rtp_stream); 1068 | 1069 | } 1070 | 1071 | 1072 | // la lista contiene i pkt ordinati con seq in ordine crescente. 1073 | // quindi, dal piu' vecchio all'ultimo. flushiamo i pkt nella lista 1074 | // fino a quando si verifica almeno una di queste due condizioni: 1075 | // 1. non ci sono salti fra i sequence (0 pkt persi) 1076 | // 2. il pkt e' stato nel buffer per un tempo >= di buf_timeout 1077 | 1078 | LIST_FOREACH_SAFE(rtpbuf, &rtp_stream->pkts, l, rtpbuf2) 1079 | { 1080 | 1081 | rtphdr = (rtp_hdr_t *)(rtpbuf->pktrtp.pcap.pkt+rtpbuf->pktrtp.hdroff); 1082 | 1083 | // nota: se seq==0, allora il primo prevseq e' 65535 e fallirebbe_ 1084 | // il check se facciamo soltanto seq+1 perche non ce' casting a u_int16_t. 1085 | // quindi, usiamo (u_int16_t)(seq+1): se seq==65535, risulta 0. 1086 | 1087 | if (((u_int16_t)(rtp_stream->last_seq_flhd+1) == ntohs(rtphdr->seq)) || 1088 | timeout(&pcap_time, &rtpbuf->pktrtp.pcap.hdr.ts,buf_timeout) 1089 | ) 1090 | { 1091 | sig_lock(); 1092 | 1093 | // if (sequence ok OR timeout) .. 1094 | 1095 | if (rtp_stream->pdump) 1096 | pcap_dump((u_char *)rtp_stream->pdump, &rtpbuf->pktrtp.pcap.hdr, rtpbuf->pktrtp.pcap.pkt); 1097 | rtp_stream->pktcount_flhd++; 1098 | rtp_stream->pktcount_inbuf--; 1099 | 1100 | rtp_streams.pktcount++; 1101 | 1102 | if (rtp_stream->last_seq_flhd == ((u_int16_t)(0-1))) 1103 | j = 1; 1104 | else 1105 | j=ntohs(rtphdr->seq)-rtp_stream->last_seq_flhd; // usata anche per fill gaps! 1106 | 1107 | rtp_stream->pktcount_lost += j-1; 1108 | rtp_streams.pktcount_lost+= j-1; 1109 | 1110 | 1111 | if (o.dump_raw) 1112 | { 1113 | if (o.fill_gaps) 1114 | { 1115 | // if j == 1 (state of "no lost packets"), writes the pkt only 1 time 1116 | for (i=0;ipktrtp.pcap.pkt + rtpbuf->pktrtp.payload.off, 1, rtpbuf->pktrtp.payload.len, rtp_stream->raw); 1118 | } 1119 | else 1120 | { 1121 | // writes the pkt 1122 | fwrite(rtpbuf->pktrtp.pcap.pkt + rtpbuf->pktrtp.payload.off, 1, rtpbuf->pktrtp.payload.len, rtp_stream->raw); 1123 | } 1124 | } 1125 | 1126 | rtp_stream->last_seq_flhd=ntohs(rtphdr->seq); 1127 | LIST_REMOVE(rtpbuf, l); 1128 | SAFE_FREE(rtpbuf->pktrtp.pcap.pkt); 1129 | SAFE_FREE(rtpbuf); 1130 | sig_unlock(); 1131 | 1132 | } 1133 | else 1134 | break; 1135 | 1136 | } 1137 | 1138 | 1139 | //VLOG(1,1,"done."); 1140 | 1141 | } 1142 | 1143 | 1144 | char *find_stream_rtp_pt(int pt, int short_vals) 1145 | { 1146 | int i; 1147 | 1148 | 1149 | if (short_vals) 1150 | { 1151 | for (i=0;rtp_payload_type_short_vals[i].str;i++) 1152 | if (rtp_payload_type_short_vals[i].type == pt) 1153 | { 1154 | return rtp_payload_type_short_vals[i].str; 1155 | } 1156 | return "?"; 1157 | } 1158 | else 1159 | { 1160 | for (i=0;rtp_payload_type_vals[i].str;i++) 1161 | if (rtp_payload_type_vals[i].type == pt) 1162 | { 1163 | return rtp_payload_type_vals[i].str; 1164 | } 1165 | return "Unknown"; 1166 | } 1167 | return "Unknown"; 1168 | } 1169 | 1170 | 1171 | void 1172 | rtp_stream_open_files(struct rtp_stream_entry *rtp_stream) 1173 | { 1174 | int count; 1175 | char pathname[PATH_MAX]; 1176 | 1177 | 1178 | sig_lock(); 1179 | 1180 | snprintf(pathname, PATH_MAX, "rtp.%d.",ndxlog); 1181 | 1182 | get_next_name(o.outdir, pathname, ".txt", &count); 1183 | 1184 | if (count == -1) 1185 | FATAL("get_next_name(...): %s", strerror(errno)); 1186 | 1187 | rtp_stream->fid = count; 1188 | 1189 | if (o.dump_pcap) 1190 | { 1191 | snprintf(pathname, PATH_MAX, "%s/rtp.%d.%d.pcap",o.outdir, ndxlog, rtp_stream->fid ); 1192 | if (!(rtp_stream->pdump = pcap_dump_open(mypcap, pathname))) 1193 | FATAL("pcap_dump_open(): %s", pcap_geterr(mypcap)); 1194 | } 1195 | 1196 | snprintf(pathname, PATH_MAX, "%s/rtp.%d.%d.txt",o.outdir, ndxlog, rtp_stream->fid ); 1197 | 1198 | LOG(1,1,"open di %s", pathname); 1199 | 1200 | if (!(rtp_stream->f = fopen(pathname, "w"))) 1201 | FATAL("fopen(): %s", strerror(errno)); 1202 | 1203 | if (o.dump_wav) 1204 | { 1205 | // open rtp_stream->player output file 1206 | snprintf(pathname, PATH_MAX, "%s/rtp.%d.html",o.outdir, ndxlog ); 1207 | 1208 | // Set Command if codec is found 1209 | if (strstr(find_stream_rtp_pt(rtp_stream->payload_type,1), "?") == NULL) { 1210 | char namebody[64], codec[64]; 1211 | snprintf(namebody, sizeof(namebody),"%s/rtp.%d.%d",o.outdir,ndxlog, rtp_stream->fid ); 1212 | snprintf(codec, sizeof(codec),"%s",find_stream_rtp_pt(rtp_stream->payload_type,1) ); 1213 | 1214 | if (strstr(codec, "711A") != NULL) { 1215 | // contains g711a 1216 | // snprintf(rtp_stream->command, sizeof(rtp_stream->command), "sox -r8000 -c1 -t al %s/rtp.%d.%d-%s.raw -t wav %s/audio-%d.wav",o.outdir, ndxlog, rtp_stream->fid, find_stream_rtp_pt(rtp_stream->payload_type,1), o.outdir, rtp_stream->fid); 1217 | snprintf(rtp_stream->command, sizeof(rtp_stream->command), "ffmpeg -nostats -loglevel 0 -acodec pcm_alaw -f alaw -ar 8000 -i %s.%s -ar 8000 %s.mp3" 1218 | ";ffmpeg -nostats -loglevel 0 -i %s.mp3 -ac 1 -filter:a aresample=8000 -map 0:a -c:a pcm_s16le -f data - | gnuplot -p -e \"set terminal png transparent size 980,60 enhanced;set output '%s.mp3.png';unset key;unset tics;unset border;set lmargin 0;set rmargin 0;set tmargin 0.5;set bmargin 0.5; plot 'command, sizeof(rtp_stream->command), "sox -r8000 -c1 -t ul %s/rtp.%d.%d-%s.raw -t wav %s/audio-%d.wav",o.outdir, ndxlog, rtp_stream->fid, find_stream_rtp_pt(rtp_stream->payload_type,1), o.outdir, rtp_stream->fid); 1224 | snprintf(rtp_stream->command, sizeof(rtp_stream->command), "ffmpeg -nostats -loglevel 0 -acodec pcm_mulaw -f mulaw -ar 8000 -i %s.%s -ar 8000 %s.mp3" 1225 | ";ffmpeg -nostats -loglevel 0 -i %s.mp3 -ac 1 -filter:a aresample=8000 -map 0:a -c:a pcm_s16le -f data - | gnuplot -p -e \"set terminal png transparent size 980,60 enhanced;set output '%s.mp3.png';unset key;unset tics;unset border;set lmargin 0;set rmargin 0;set tmargin 0.5;set bmargin 0.5; plot 'command, sizeof(rtp_stream->command), "ffmpeg -nostats -loglevel 0 -acodec g729 -f g729 -i %s.%s %s.mp3" 1231 | ";ffmpeg -nostats -loglevel 0 -i %s.mp3 -ac 1 -filter:a aresample=8000 -map 0:a -c:a pcm_s16le -f data - | gnuplot -p -e \"set terminal png transparent size 980,60 enhanced;set output '%s.mp3.png';unset key;unset tics;unset border;set lmargin 0;set rmargin 0;set tmargin 0.5;set bmargin 0.5; plot 'command, sizeof(rtp_stream->command), "ffmpeg -nostats -loglevel 0 -acodec %s -f %s -i %s.%s %s.mp3" 1236 | ";ffmpeg -nostats -loglevel 0 -i %s.mp3 -ac 1 -filter:a aresample=8000 -map 0:a -c:a pcm_s16le -f data - | gnuplot -p -e \"set terminal png transparent size 980,60 enhanced;set output '%s.mp3.png';unset key;unset tics;unset border;set lmargin 0;set rmargin 0;set tmargin 0.5;set bmargin 0.5; plot 'RTP HTML5 Player
" ); 1372 | // after this fclose, no more fprint to player!! 1373 | SAFE_FCLOSE(o.player); 1374 | } 1375 | 1376 | if (rtp_stream->rev && rtp_stream->rev->rev) 1377 | rtp_stream->rev->rev = NULL; 1378 | 1379 | SAFE_FREE(rtp_stream); 1380 | } 1381 | 1382 | 1383 | void rtp_streams_close() 1384 | { 1385 | VLOG(1,1,"closing streams..."); 1386 | 1387 | while (rtp_streams.list.lh_first) 1388 | rtp_stream_close(rtp_streams.list.lh_first); 1389 | 1390 | } 1391 | 1392 | 1393 | void help() 1394 | { 1395 | printf("Copyright (c) 2007-2008 Dallachiesa Michele \n"); 1396 | printf("Copyright (c) 2015 QXIP BV \n"); 1397 | printf("rtpbreakr v%s is free software, covered by the GNU General Public License.\n\n", VERSION); 1398 | printf("USAGE: rtpbreakr (-r|-i) [options]\n"); 1399 | 1400 | printf("\n INPUT\n\n"); 1401 | printf(" -r Read packets from pcap file \n"); 1402 | printf(" -i Read packets from network interface \n"); 1403 | printf(" -L Force datalink header length == bytes\n"); 1404 | printf("\n OUTPUT\n\n"); 1405 | printf(" -d Set output directory to (def:%s)\n",DEFAULT_OUTDIR); 1406 | printf(" -w Disable RTP raw dumps\n"); 1407 | printf(" -W Disable RTP pcap dumps\n"); 1408 | printf(" -W Disable RTP external dumps\n"); 1409 | printf(" -g Fill gaps in RTP raw dumps (caused by lost packets)\n"); 1410 | printf(" -n Dump noise packets\n"); 1411 | printf(" -f Disable stdout logging\n"); 1412 | printf(" -F Enable syslog logging\n"); 1413 | printf(" -v Be verbose\n"); 1414 | printf("\n SELECT\n\n"); 1415 | printf(" -m Sniff packets in promisc mode\n"); 1416 | printf(" -p Add pcap filter \n"); 1417 | printf(" -e Expect even destination UDP port\n"); 1418 | printf(" -u Expect unprivileged source/destination UDP ports (>1024)\n"); 1419 | printf(" -y Expect RTP payload type == \n"); 1420 | printf(" -l Expect RTP payload length == bytes\n"); 1421 | printf(" -t Set packet timeout to seconds (def:%.2f)\n", PKT_TIMEOUT); 1422 | printf(" -T Set pattern timeout to seconds (def:%.2f)\n", RTP_STREAM_PATTERN_TIMEOUT); 1423 | printf(" -P Set pattern packets count to (def:%d)\n", o.pattern_pkts); 1424 | printf("\n EXECUTION\n\n"); 1425 | printf(" -Z Run as user \n"); 1426 | printf(" -D Run in background (option -f implicit)\n"); 1427 | printf("\n MISC\n\n"); 1428 | printf(" -k List known RTP payload types\n"); 1429 | printf(" -h This\n"); 1430 | printf("\n"); 1431 | 1432 | exit(0); 1433 | } 1434 | 1435 | 1436 | void sig_stats_handler(int signo) 1437 | { 1438 | struct rtp_stream_entry *rtp_stream; 1439 | int s; 1440 | int n; 1441 | 1442 | LOG(1,1," + Status"); 1443 | LOG(1,1," Alive RTP Sessions: %d", rtp_streams.active); 1444 | LOG(1,1," Closed RTP Sessions: %d", rtp_streams.closed); 1445 | LOG(1,1," Detected RTP Sessions: %d", rtp_streams.active + rtp_streams.closed); 1446 | LOG(1,1," Flushed RTP packets: %d", rtp_streams.pktcount); 1447 | LOG(1,1," Lost RTP packets: %d (%.2f%%)", rtp_streams.pktcount_lost, 1448 | PERCENTAGE(rtp_streams.pktcount_lost,rtp_streams.pktcount + rtp_streams.pktcount_lost)); 1449 | 1450 | LOG(1,1," Noise (false positive) packets: %d", rtp_streams.pktcount_noise); 1451 | 1452 | n = 0; 1453 | 1454 | LIST_FOREACH(rtp_stream, &rtp_streams.list, l) 1455 | { 1456 | if (!rtp_stream->pattern_found) 1457 | continue; 1458 | 1459 | n++; 1460 | s =rtp_stream->last_pkt.tv_sec - rtp_stream->first_pkt.tv_sec; 1461 | LOG(1,0," + [rtp%d] stats: ", rtp_stream->fid); 1462 | print_stream_stat(rtp_stream); 1463 | } 1464 | 1465 | if (n == 0) 1466 | LOG(1,1," + No active RTP streams"); 1467 | 1468 | 1469 | } 1470 | 1471 | 1472 | void rtp_streams_timeout() 1473 | { 1474 | static struct timeval last_run = 1475 | { 1476 | 0,0 1477 | }; 1478 | struct rtp_stream_entry *rtp_stream, *rtp_stream2; 1479 | 1480 | 1481 | if (!timeout(&pcap_time,&last_run,o.timeout_pattern)) 1482 | return; 1483 | last_run = pcap_time; 1484 | 1485 | 1486 | LIST_FOREACH_SAFE(rtp_stream, &rtp_streams.list, l, rtp_stream2) 1487 | { 1488 | 1489 | if (rtp_stream->pattern_found) 1490 | { 1491 | if (timeout(&pcap_time, &rtp_stream->last_pkt,o.timeout_pkt)) 1492 | { 1493 | rtp_stream_close(rtp_stream); 1494 | continue; 1495 | } 1496 | } 1497 | else 1498 | { 1499 | if (timeout(&pcap_time, &rtp_stream->last_pkt,o.timeout_pattern)) 1500 | { 1501 | rtp_stream_close(rtp_stream); 1502 | continue; 1503 | } 1504 | } 1505 | 1506 | } 1507 | 1508 | //LOG(1,1,"done."); 1509 | } 1510 | 1511 | 1512 | int timeout(struct timeval *a, struct timeval *b, float t) 1513 | { 1514 | struct timeval c; 1515 | float r; 1516 | 1517 | 1518 | TIMEVAL_SUB(a, b, &c); 1519 | 1520 | r = c.tv_sec + (float)c.tv_usec /1000000; 1521 | 1522 | //LOG(1,1,"a:%f b:%f a-b:%f , a-b:%f", a->tv_sec + (float)a->tv_usec /1000000,b->tv_sec +(float) b->tv_usec /1000000, c.tv_sec + (float)c.tv_usec /1000000,r); 1523 | 1524 | return r > t ? 1 : 0; 1525 | } 1526 | 1527 | 1528 | void rtp_stream_search_rev(struct rtp_stream_entry *rtp_stream) 1529 | { 1530 | struct rtp_stream_entry *rtp_stream2; 1531 | 1532 | 1533 | LIST_FOREACH(rtp_stream2, &rtp_streams.list, l) 1534 | { 1535 | if (!rtp_stream2->pattern_found || rtp_stream2->rev) 1536 | continue; 1537 | if (rtp_stream2 != rtp_stream && 1538 | rtp_stream2->addrs.srcaddr == rtp_stream->addrs.dstaddr && 1539 | rtp_stream2->addrs.dstaddr == rtp_stream->addrs.srcaddr) 1540 | { 1541 | LOG(1,1," * [rtp%d] probable reverse RTP stream: [rtp%d]",rtp_stream->fid, rtp_stream2->fid); 1542 | 1543 | rtp_stream->rev = rtp_stream2; 1544 | rtp_stream2->rev = rtp_stream; 1545 | 1546 | SAFE_FPRINTF(rtp_stream->f,"Probable reverse RTP stream id: rtp.%d.%d\n",ndxlog,rtp_stream->rev->fid); 1547 | 1548 | } 1549 | } 1550 | 1551 | } 1552 | 1553 | 1554 | /* EOF */ 1555 | 1556 | --------------------------------------------------------------------------------