Commit 33110ad7 authored by 's avatar
Browse files

test_packet_read supports IPv6 and MPLS


git-svn-id: file:///home/svn/mapi/trunk@223 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent d31244ef
......@@ -8,14 +8,16 @@ int main(MAPI_UNUSED int argc, char *argv[])
int fd;
int fid;
struct mapipkt *pkt;
int packet_no;
int packet_no, packet_ipv4_no, packet_ipv6_no,
packet_mpls_no, packet_nonip_no;
unsigned char *packet;
unsigned char *p;
int i, j;
unsigned int l3_proto;
if(argc!=3)
if(argc<2 || argc>3)
{
printf("\nWrong arguments\n");
printf("\ntest_packet_read <device> [\"<BPF filter string>\"]\n");
return -1;
}
......@@ -25,44 +27,109 @@ int main(MAPI_UNUSED int argc, char *argv[])
return -1;
}
fid=mapi_apply_function(fd,"BPF_FILTER", argv[2]);
if (argc>2)
fid=mapi_apply_function(fd,"BPF_FILTER", argv[2]);
fid=mapi_apply_function(fd, "TO_BUFFER");
mapi_connect(fd);
packet_no=0;
packet_no=packet_ipv4_no=packet_ipv6_no=
packet_mpls_no=packet_nonip_no=0;
while(1)
{
// sleep(5);
if((pkt=mapi_get_next_pkt(fd, fid)) != NULL)
{
packet=&(pkt->pkt);
packet_no++;
/* Check L3 protocol */
p=packet;
p+=12;
l3_proto=((*p)<<8 | *(p+1)) & 0xFFFF;
if (l3_proto==0x0800)
packet_ipv4_no++;
else if (l3_proto==0x86DD)
packet_ipv6_no++;
else if (l3_proto==0x8847)
packet_mpls_no++;
else
packet_nonip_no++;
if ((packet_no%1)==0)
printf("Got packet: %d\n", packet_no);
printf("packet_no: %d, packet_ipv4_no: %d, packet_ipv6_no: %d, packet_mpls_no: %d, packet_nonip_no: %d\n", packet_no, packet_ipv4_no, packet_ipv6_no, packet_mpls_no, packet_nonip_no);
printf("ts: %llu, ifindex: %u, caplen: %u, wlen: %u\n", pkt->ts, pkt->ifindex, pkt->caplen, pkt->wlen);
packet=&(pkt->pkt);
/* Check Ethernet type */
p=packet;
p+=12;
/* Print L3 protocol */
if (l3_proto==0x0800)
printf("L3 protocol: IPv4\n");
else if (l3_proto==0x86DD)
printf("L3 protocol: IPv6\n");
else if (l3_proto==0x8847)
printf("L3 protocol: MPLS\n");
else
printf("L3 protocol: non-IP (0x%04x)\n", l3_proto);
/* Print Ethernet addresses */
p = packet;
p+=(ETH_HLEN+12);
printf("src IP: ");
for (j = 0; j < 4; j++) {
printf("%03d", *p++);
if (j<3)
printf(".");
p+=6;
printf("src Eth: ");
for (j = 0; j < 6; j++) {
printf("%02x", *p++);
if (((j+1)%2)==0)
printf(":");
}
printf(" dst IP: ");
for (j = 0; j < 4; j++) {
printf("%03d", *p++);
if (j<3)
printf(".");
else
printf("\n");
p = packet;
printf(" dst Eth: ");
for (j = 0; j < 6; j++) {
printf("%02x", *p++);
if (((j+1)%2)==0)
printf(":");
}
printf("\n");
/* Print IPv4 addresses */
if (l3_proto==0x0800 || l3_proto==0x8847) {
p = packet;
p+=(ETH_HLEN+12);
/* Skip 4-byte MPLS header */
if (l3_proto==0x8847)
p+=4;
printf("src IP: ");
for (j = 0; j < 4; j++) {
printf("%03d", *p++);
if (j<3)
printf(".");
}
printf(" dst IP: ");
for (j = 0; j < 4; j++) {
printf("%03d", *p++);
if (j<3)
printf(".");
else
printf("\n");
}
}
/* Print IPv6 addresses */
else if (l3_proto==0x86DD) {
p = packet;
p+=(ETH_HLEN+8);
printf("src IP: ");
for (j = 0; j < 16; j++) {
printf("%02x", *p++);
if (((j+1)%2)==0)
printf(":");
}
printf(" dst IP: ");
for (j = 0; j < 16; j++) {
printf("%02x", *p++);
if (((j+1)%2)==0)
printf(":");
}
printf("\n");
}
p=packet;
for (i=0; i<40; i++) {
......@@ -70,6 +137,7 @@ int main(MAPI_UNUSED int argc, char *argv[])
if (((i+1)%4)==0)
printf(" ");
}
printf("\n");
}
else
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment