-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcap_routines.c
244 lines (209 loc) · 6.39 KB
/
pcap_routines.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* =====================================================================================
*
* Filename: pcap_routines.c
*
* Description:
*
* Version: 1.0
* Created: 12/22/2014 11:47:56 AM
* Revision: none
* Compiler: gcc
*
* Author: nikkolasg (mn),
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <signal.h>
#include "pcap_routines.h"
#include "arp.h"
#define SNAP_LEN 1518
#define MAX_PKT_NUMBER 5
/* the main structure used by pcap */
static pcap_t * handle;
/* used to filter the incoming packets by pcap */
struct bpf_program bpf;
Packet_analyzer arp_analyzer = NULL;
Packet_analyzer ip_analyzer = NULL;
extern int initialized = 0;
static int packet_count = 0;
/**
* Send a raw array of bytes
* return 0 in success, -1 in failure
* */
int pcap_send_packet(const u_char * bytes,int size) {
if(pcap_inject(handle,bytes,size) != size) {
fprintf(stderr,"Packet could not be sent.\n");
return -1;
}
return 0;
}
/*
* === FUNCTION ======================================================================
* Name: pcap_set_arp_analyzer
* Description: Simply sets the arp pointer function right
* so that function receives the packets when sniffing later.
* =====================================================================================
*/
void
pcap_set_arp_analyzer ( Packet_analyzer arp )
{
arp_analyzer = arp;
return ;
} /* ----- end of function pcap_set_arp_analyzer ----- */
void
pcap_set_ip_analyzer ( Packet_analyzer ip )
{
ip_analyzer = ip;
return ;
} /* ----- end of function pcap_set_ip_analyzer ----- */
/**
* Main loop function that receives packets
* */
void sniff_callback(u_char * user, const struct pcap_pkthdr * h,const u_char * bytes) {
int i = 0;
printf("Received packet number %d ==> %d bytes\n",packet_count++,h->len);
const struct pkt_eth * eth;
unsigned short eth_type;
unsigned int captureLength = h->caplen;
unsigned int packetLength = h->len;
if(captureLength != packetLength) {
fprintf(stderr,"Error : received packet with %d available instead of %d \n",captureLength,packetLength);
return;
}
if(captureLength < ETH_SIZE) {
fprintf(stderr,"Error : received too small packet , %d bytes",captureLength);
return;
}
eth = (struct pkt_eth*)(bytes);
// print the packet
// print_packet(bytes);
eth_type = ntohs(eth->type);
if(eth_type == ETHERTYPE_ARP && arp_analyzer != NULL) {
arp_analyzer(bytes,h->len);
} else if (eth_type == ETHERTYPE_IP && ip_analyzer != NULL) {
ip_analyzer(bytes,h->len);
}
//printf("\n");for(i=0; i < 25; i++) { printf("-"); }; printf("\n\n");
return;
}
/* returns 0 if everything went well */
int set_options(pcap_t * handle) {
int ret = 0;
printf("Options : ");
ret = pcap_set_promisc(handle,1);
if(ret != 0) {
fprintf(stderr,"Error setting promiscuous mode\n");
return ret;
}
printf("promisc");
ret = pcap_set_snaplen(handle,SNAP_LEN);
if(ret != 0) {
fprintf(stderr,"Error setting snapshot length\n");
return ret;
}
printf(", snaplen");
ret = pcap_set_timeout(handle,1000);
if(ret != 0) {
fprintf(stderr,"Error setting timeout\n");
return ret;
}
printf(", timeout ");
return ret;
}
/* simply activate the interface we're listening on */
int activate(pcap_t * handle) {
int ret = pcap_activate(handle);
switch(ret) {
case 0:
fprintf(stdout,"Activation complete\n");
break;
case PCAP_WARNING_PROMISC_NOTSUP:
fprintf(stderr,"Promiscuous mode not supported\n");
return ret;
case PCAP_ERROR_PERM_DENIED:
fprintf(stderr,"Not have the permission required\n");
return ret;
/* case PCAP_ERROR_PROMISC_PERM_DENIED:
fprintf(stderr,"Not have the permission required for promiscuous\n");
return ret;
*/default:
fprintf(stderr,"Error occured during activation, see code\n");
return ret;
}
return ret;
}
/* Will activate device , filter & call the sniffing loop */
/* Return 0 on success otherwise exit application */
int pcap_init(char * interface, char * filter) {
char err[PCAP_ERRBUF_SIZE]; //error buffer
bpf_u_int32 mask; // network mask
bpf_u_int32 ip; // network ip
struct in_addr addr; // network number
int ret;
if(initialized) return;
/* get mask & ip */
if(pcap_lookupnet(interface, &ip, &mask, err) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n",interface,err);
exit(EXIT_FAILURE);
}
handle = pcap_create(interface,err);
if (handle == NULL) {
fprintf(stderr,"Error pcap_create() : %s \n",err);
exit(EXIT_FAILURE);
}
if(set_options(handle) != 0) {
fprintf(stderr,"Exiting\n");
exit(EXIT_FAILURE);
}
if (activate(handle) != 0) {
fprintf(stderr,"Exiting\n");
exit(EXIT_FAILURE);
}
/* FILTER PART */
if(filter != NULL) {
if(pcap_compile(handle,&bpf,filter,0,ip) == -1){
fprintf(stderr,"Couldn't compile filter expr %s : %s\n",filter,pcap_geterr(handle));
exit(EXIT_FAILURE);
}
if(pcap_setfilter(handle, &bpf) == -1) {
fprintf(stderr,"Couldn't install filter %s : %s\n",filter,pcap_geterr(handle));
exit(EXIT_FAILURE);
}
}
initialized = 1;
return 0;
}
/*
* === FUNCTION ======================================================================
* Name: pcap_exit
* Description: free up the memory
* =====================================================================================
*/
void
pcap_exit_ (void )
{
pcap_breakloop(handle);
pcap_freecode(&bpf);
pcap_close(handle);
return ;
} /* ----- end of function pcap_close ----- */
/*
* === FUNCTION ======================================================================
* Name: pcap_sniff
* Description: simply a wrapper around the sniffing method of pcap
* =====================================================================================
*/
void
pcap_sniff ( int packet_count )
{
printf("Sniffing started ...");
pcap_loop(handle,packet_count,sniff_callback,NULL);
printf(" Ok ;)");
fflush(stdout);
return ;
} /* ----- end of function pcap_sniff ----- */