aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/vTC/apexlake/experimental_framework/libraries/packet_checker/test_sniff.c
blob: a4eda3cfff67fe37cc5ae5bc502c71f722d36611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) 2015 Intel Research and Development Ireland Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <pcap.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>

static int expected_tos = -1;
static int cmatch = 0;


/* 4 bytes IP address */
typedef struct ip_address
{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;
} ip_address;


/* IPv4 header */
typedef struct ip_header
{
    u_char      ver_ihl;
    u_char      tos;
    u_short     tlen;
    u_short     identification;
    u_short     flags_fo;
    u_char      ttl;
    u_char      proto;
    u_short     crc;
    ip_address  saddr;
    ip_address  daddr;
    u_int       op_pad;
} ip_header;


/* UDP header*/
typedef struct udp_header
{
    u_short    sport;          // Source port
    u_short    dport;          // Destination port
    u_short    len;            // Datagram length
    u_short    crc;            // Checksum
} udp_header;


/* Save results on file */
void save_and_exit(int sig)
{
    write_file();
	exit(0);
}


/*
 * This callback function is called for each received packet
 */
void stats_collection(u_char *useless,
                      const struct pcap_pkthdr* pkthdr,
                      const u_char* packet)
{
    ip_header *ih;
    udp_header *uh;
    u_int ip_len;
    ih = (ip_header *) (packet + 14);
    ip_len = (ih->ver_ihl & 0xf) * 4;
    u_char tos = ih->tos;
    // Counter update
    if(tos==expected_tos)
        cmatch ++;
}


int main(int argc,char **argv)
{
    int i;
    char *dev;
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t* descr;
    const u_char *packet;
    struct pcap_pkthdr hdr;
    struct ether_header *eptr;

    if(argc != 3)
    {
        fprintf(stdout,"Usage: %s interface_name expected_tos\n", argv[0]);
        exit(1);
    }

    expected_tos = atoi(argv[2]);

    /* Setup signal to stop the sniffer */
	signal(SIGTERM, save_and_exit);

    /* Take a device to read from */
    dev = argv[1];
    if(dev == NULL)
    {
        printf("%s\n",errbuf);
        exit(1);
    }

    /* Open device for reading */
    descr = pcap_open_live(dev, BUFSIZ, 0, -1, errbuf);
    if(descr == NULL)
    {
        printf("pcap_open_live(): %s\n", errbuf);
        exit(1);
    }

    /* Start the loop to be run for each packet */
    pcap_loop(descr, -1, stats_collection, NULL);
    return 0;
}


int write_file()
{
    FILE *f = fopen("/tmp/apexlake/results/packet_checker.res", "w");
    if (f == NULL)
    {
        printf("Error opening file!\n");
        exit(1);
    }
    fprintf(f, "%d\n", cmatch);
    fclose(f);
}