-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcap_track.c
161 lines (138 loc) · 3.38 KB
/
pcap_track.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <net/ethernet.h>
#include <openflow/openflow.h>
#include "pcap_track.h"
#include "utils.h"
#ifndef PTRACK_MAX_LEN
#define PTRACK_MAX_LEN 256
#endif
#ifndef ETHERTYPE_VLAN
#define ETHERTYPE_VLAN 0x8100
#endif
struct ptrack_entry {
char * data;
int len;
struct pcap_pkthdr hdr;
};
struct ptrack_list {
struct ptrack_entry entries[PTRACK_MAX_LEN];
int count, head;
};
static unsigned int tcp_payload_offset(void * data, int len);
/*************************************************************/
struct ptrack_list * ptrack_new()
{
struct ptrack_list * neo;
neo = malloc_and_check(sizeof(struct ptrack_list));
bzero(neo,sizeof(struct ptrack_list));
return neo;
}
/***********************************************************
* FIXME: make it possible to track non-openflow streams
***/
int ptrack_add_of_entry( struct ptrack_list * ptl, void * data, int len, struct pcap_pkthdr hdr)
{
struct ptrack_entry * ent;
int add_count=0;
int add_len;
int offset = tcp_payload_offset(data, len);
if(offset >= len) // nothing to add
return add_count;
len-=offset; // jump to just the tcp payload part
data+=offset;
while(len>0 )
{
struct ofp_header * ofph= data;
add_len= ntohs(ofph->length);
if(add_len > len)
{
fprintf(stderr, "ptrack_add_entry: unable to track timestamp for partial openflow message\n");
return add_count;
}
ent = &ptl->entries[ptl->head];
ent->data = malloc_and_check(add_len);
memcpy(ent->data, data, add_len);
ent->len = add_len;
ent->hdr=hdr;
ptl->head++;
if(ptl->head >= PTRACK_MAX_LEN)
ptl->head=0;
if(ptl->count < PTRACK_MAX_LEN)
ptl->count++;
add_count++;
len -=add_len;
data += add_len;
}
return add_count;
}
/********************************************************/
int ptrack_lookup(struct ptrack_list * ptl, void * data, int len, struct pcap_pkthdr * hdr)
{
int i, idx;
struct ptrack_entry * ent;
for( i = 0 ; i < ptl->count ; i ++)
{
idx = ptl-> head -1 - ptl->count;
if(idx<0)
idx+=PTRACK_MAX_LEN;
ent = & ptl->entries[idx];
if((len <= ent-> len) && (memcmp(data, ent->data, MIN(len,ent->len))==0))
{
*hdr = ent->hdr;
ptl->head--;
if(ptl->head<0)
ptl->head+=PTRACK_MAX_LEN;
ptl->count--;
assert(ptl->count >=0);
return 1;
}
}
return 0;
}
/*********************************************************/
void ptrack_free(struct ptrack_list * ptl)
{
int i, idx;
struct ptrack_entry * ent;
for(i = 0; i < ptl->count ; i ++)
{
idx = ptl-> head -1 - ptl->count;
if(idx<0)
idx+=PTRACK_MAX_LEN;
ent = & ptl->entries[idx];
free(ent->data);
}
free(ptl);
}
/*********************************************************/
static unsigned int tcp_payload_offset(void * data, int len)
{
int offset=0;
struct iphdr * ip;
struct tcphdr * tcp;
struct ether_header * eth = data+offset;
if(eth->ether_type == htons(ETHERTYPE_VLAN))
{
offset+=4;
eth = data+offset;
}
if((offset>=len)||(eth->ether_type != htons(ETHERTYPE_IP)))
return INT_MAX;
offset+=sizeof(struct ether_header);
ip = data + offset;
if((offset>=len) || (ip->protocol != IPPROTO_TCP))
return INT_MAX;
offset += ip->ihl *4;
tcp = data + offset;
offset += tcp->doff*4;
return offset;
}