Skip to content

Commit

Permalink
pcap2mjr: avoid misaligned access
Browse files Browse the repository at this point in the history
  • Loading branch information
atoppi committed Nov 16, 2023
1 parent e274a93 commit c81a0e0
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions postprocessing/pcap2mjr.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ int main(int argc, char *argv[])
sizeof(struct udphdr) + 12, pkt_size = 0;
gboolean header_written = FALSE;
gint64 start_ts = 0, pkt_ts = 0;
pcap2mjr_ethernet_header eth;
struct sll_header lcc;
struct ip v4;
struct ip6_hdr v6;
janus_pp_rtp_header rtp;
while(working && (ret = pcap_next_ex(pcap, &header, &buffer)) >= 0) {
count++;
if(header->len != header->caplen) {
Expand All @@ -222,27 +227,27 @@ int main(int argc, char *argv[])
int protocol = 0;
if(link == DLT_EN10MB) {
/* Ethernet */
pcap2mjr_ethernet_header *eth = (pcap2mjr_ethernet_header *)temp;
protocol = ntohs(eth->type);
memcpy(&eth, temp, sizeof(pcap2mjr_ethernet_header));
protocol = ntohs(eth.type);
temp += sizeof(pcap2mjr_ethernet_header);
pkt_size -= sizeof(pcap2mjr_ethernet_header);
} else {
/* Linux Cooked Capture */
struct sll_header *lcc = (struct sll_header *)temp;
protocol = ntohs(lcc->sll_protocol);
memcpy(&lcc, temp, sizeof(struct sll_header));
protocol = ntohs(lcc.sll_protocol);
temp += sizeof(struct sll_header);
pkt_size -= sizeof(struct sll_header);
}
if(protocol == 0x0800) {
/* IPv4 */
struct ip *v4 = (struct ip *)temp;
protocol = v4->ip_p;
memcpy(&v4, temp, sizeof(struct ip));
protocol = v4.ip_p;
temp += sizeof(struct ip);
pkt_size -= sizeof(struct ip);
} else if(protocol == 0x86DD) {
/* IPv6 */
struct ip6_hdr *v6 = (struct ip6_hdr *)temp;
protocol = v6->ip6_ctlun.ip6_un1.ip6_un1_nxt;
memcpy(&v6, temp, sizeof(struct ip6_hdr));
protocol = v6.ip6_ctlun.ip6_un1.ip6_un1_nxt;
temp += sizeof(struct ip6_hdr);
pkt_size -= sizeof(struct ip6_hdr);
} else {
Expand All @@ -261,14 +266,14 @@ int main(int argc, char *argv[])
temp += sizeof(struct udphdr);
pkt_size -= sizeof(struct udphdr);
/* Make sure this is an RTP packet */
janus_pp_rtp_header *rtp = (janus_pp_rtp_header *)temp;
if(rtp->version != 2 || (rtp->type >= 64 && rtp->type < 96)) {
memcpy(&rtp, temp, sizeof(janus_pp_rtp_header));
if(rtp.version != 2 || (rtp.type >= 64 && rtp.type < 96)) {
if(show_warnings) {
JANUS_LOG(LOG_WARN, "Not an RTP packet, skipping packet #%"SCNu32"\n", count);
}
continue;
}
pssrc = htonl(rtp->ssrc);
pssrc = htonl(rtp.ssrc);
if(ssrc == 0) {
ssrc = pssrc;
JANUS_LOG(LOG_INFO, "Autodetected SSRC %"SCNu32"\n", ssrc);
Expand Down

0 comments on commit c81a0e0

Please sign in to comment.