-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
196 lines (172 loc) · 4.79 KB
/
main.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
#include <pcap.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "parse.h"
#include "dispatch.h"
#include "utils.h"
#define BPF_FILTER_SIZE 1024
#define NO_FILTER 0
#define HAS_FILTER 1
// GLOBAL DEVICE
ao_device *main_device;
void pkt_handler(u_char *args_from_loop, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
int type = -1;
u_char *data;
data = link_layer_dispatch((u_char *) pkt_data, &type, ETHERNET_LINK);
if (type == NO_UPPER_LAYER)
{
fprintf(stderr, "[DEBUG] Link: No Upper Layer\n");
return;
}
if (data == NULL)
{
fprintf(stderr, "[DEBUG] Link: Skip\n");
return;
}
data = network_layer_dispatch(data, &type);
if (type == NO_UPPER_LAYER)
{
fprintf(stderr, "[DEBUG] Network : No Upper Layer\n");
return;
}
if (data == NULL)
{
fprintf(stderr, "[DEBUG] Network : Skip\n");
return;
}
data = transport_layer_dispatch(data, &type);
if (type == NO_UPPER_LAYER)
{
fprintf(stderr, "[DEBUG] Transport : No Upper Layer\n");
return;
}
if (data == NULL)
{
fprintf(stderr, "[DEBUG] Transport : Skip\n");
return;
}
}
int main(int argc, char *argv[])
{
// Cmd Args
int ch;
// Pcap Info
pcap_t *handle;
char errbuf[PCAP_ERRBUF_SIZE];
// filter
struct bpf_program fp;
char filter_exp[BPF_FILTER_SIZE] = {0};
int has_filter = NO_FILTER;
// Network Info
char *dev;
int link_layer_type_code;
bpf_u_int32 mask;
bpf_u_int32 net;
// packet setting
struct pcap_pkthdr header;
const u_char *packet;
/*--------------- CMD ISSUE ----------------*/
/*------------------------------------------*/
// init some args
dev = NULL;
link_layer_type_code = ETHERNET_LINK;
while ((ch = getopt(argc, argv, "f:t:i:h")) != -1)
{
switch (ch)
{
case 'i':
printf("Manual Chose NetCard : '%s'\n", (char *) optarg);
printf("Warning : Your should set interface link type by '-t'");
printf(" Support Type in Help\n");
dev = (char *) optarg;
break;
case 't':
link_layer_type_code = atoi(optarg);
printf("Manual Set Interface Type : 0x%x\n", link_layer_type_code);
break;
case 'f':
strncpy(filter_exp, optarg, BPF_FILTER_SIZE);
has_filter = HAS_FILTER;
break;
case 'l':
printf("sorry , this will imeplment in next version\n");
break;
case 'h':
// no break; fall through
default:
printf("help");
return 0;
}
}
/* -------------- Net Filter ISSUE -----------*/
/* -------------------------------------------*/
/* Define the device */
if (NULL == dev)
{
dev = get_default_dev_name(errbuf);
}
/* Get Interface infomation */
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1)
{
fprintf(stderr, "[-]Couldn't get netmask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
}
/* open session for snifferning */
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL)
{
fprintf(stderr, "[-]Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return (2);
}
/* Compile filter */
if (has_filter == HAS_FILTER)
{
if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1)
{
fprintf(stderr, "[-]Couldn't Parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return (2);
}
if (pcap_setfilter(handle, &fp) == -1)
{
fprintf(stderr, "[-]Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
return (2);
}
}
// Aduio Handle
ao_sample_format format;
int default_driver;
char *buffer;
int buf_size;
// Create Mutex
// pthread_mutex_init(&mutex, NULL);
ao_initialize();
default_driver = ao_default_driver_id();
printf("default_driver : %d\n", default_driver);
// Set Some Audio Args
memset(&format, 0, sizeof(format));
format.bits = FORMAT_BITS;
format.channels = FORMAT_CHANNEL;
format.rate = FORMAT_RATE;
format.byte_format = BYTE_FORMAT;
// open device
main_device = ao_open_live(default_driver, &format, NULL /* no options */);
if (main_device == NULL)
{
fprintf(stderr, "Error opening device.\n");
return 1;
}
/* Grab a packet */
u_char cnt;
cnt = 0;
pcap_loop(handle, 0, pkt_handler, &cnt);
ao_close(main_device);
ao_shutdown();
// destroy mutex
// pthread_mutex_destroy(&mutex);
pcap_close(handle);
return (0);
}