Skip to content

Commit bc7b536

Browse files
committed
Allow LINUX_SLL type pcaps to be read with pcap_play.
Taken from https://code.osso.nl/projects/sipp/changeset/a781aef3e65d/
1 parent 5d4a7c7 commit bc7b536

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/prepare_pcap.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@
4141
* made available by the platform, as we had no problems to get them on all supported platforms.
4242
*/
4343

44-
typedef struct _ether_hdr {
45-
char ether_dst[6];
46-
char ether_src[6];
44+
typedef struct _ether_type_hdr {
4745
u_int16_t ether_type; /* we only need the type, so we can determine, if the next header is IPv4 or IPv6 */
48-
} ether_hdr;
46+
} ether_type_hdr;
4947

5048
typedef struct _ipv6_hdr {
5149
char dontcare[6];
@@ -99,7 +97,9 @@ int prepare_pkts(char *file, pcap_pkts *pkts)
9997
u_int16_t base = 0xffff;
10098
u_long pktlen;
10199
pcap_pkt *pkt_index;
102-
ether_hdr *ethhdr;
100+
size_t ether_type_offset;
101+
ether_type_hdr *ethhdr;
102+
103103
struct iphdr *iphdr;
104104
ipv6_hdr *ip6hdr;
105105
struct udphdr *udphdr;
@@ -110,6 +110,19 @@ int prepare_pkts(char *file, pcap_pkts *pkts)
110110
if (!pcap)
111111
ERROR("Can't open PCAP file '%s'", file);
112112

113+
switch (pcap_datalink(pcap)) {
114+
case DLT_EN10MB:
115+
/* srcmac[6], dstmac[6], ethertype[2] */
116+
ether_type_offset = 12;
117+
break;
118+
case DLT_LINUX_SLL:
119+
/* some_stuff[14], ethertype[2] */
120+
ether_type_offset = 14;
121+
break;
122+
default:
123+
ERROR("Unsupported link-type %d", pcap_datalink(pcap));
124+
}
125+
113126
#if HAVE_PCAP_NEXT_EX
114127
while (pcap_next_ex (pcap, &pkthdr, (const u_char **) &pktdata) == 1) {
115128
#else
@@ -122,7 +135,10 @@ int prepare_pkts(char *file, pcap_pkts *pkts)
122135
ERROR("Can't allocate memory for pcap pkthdr");
123136
while ((pktdata = (u_char *) pcap_next (pcap, pkthdr)) != NULL) {
124137
#endif
125-
ethhdr = (ether_hdr *)pktdata;
138+
if (pkthdr->len != pkthdr->caplen) {
139+
ERROR("You got truncated packets. Please create a new dump with -s0");
140+
}
141+
ethhdr = (ether_type_hdr *)(pktdata + ether_type_offset);
126142
if (ntohs(ethhdr->ether_type) != 0x0800 /* IPv4 */
127143
&& ntohs(ethhdr->ether_type) != 0x86dd) { /* IPv6 */
128144
fprintf(stderr, "Ignoring non IP{4,6} packet!\n");

0 commit comments

Comments
 (0)