Browse code

추가 파일 등록

Nepirity Corp authored on19/03/2018 16:32:20
Showing2 changed files

... ...
@@ -1,3 +1,4 @@
1 1
 all:
2 2
 	gcc -o list list.c -lpcap
3 3
 	gcc -o address address.c -lpcap
4
+	gcc -o sniffing sniffing.c -lpcap
4 5
new file mode 100644
... ...
@@ -0,0 +1,68 @@
1
+/*!
2
+ *  @file    sniffing.c
3
+ *  @author  HiSEON (ikseon@nepirity.com)
4
+ *
5
+ *  @internal
6
+ *       Created:  2018년 03월 20일
7
+ *     Copyright:  Copyright (c) 2018, Kang IkSeon
8
+ */
9
+
10
+#include <stdio.h>
11
+#include <string.h>
12
+
13
+#include <netinet/in.h>
14
+#include <arpa/inet.h>
15
+
16
+#include <pcap.h>
17
+
18
+void packet_handler(u_char *param,
19
+  const struct pcap_pkthdr *header, const u_char *pkt_data) {
20
+  printf("caplen : %d\n", header->caplen);
21
+  printf("len : %d\n", header->len);
22
+}
23
+
24
+int main(int argc, char **argv) {
25
+    pcap_t *adhandle;
26
+    char errbuf[PCAP_ERRBUF_SIZE];
27
+    pcap_if_t *alldevs;
28
+    pcap_if_t *d;
29
+    struct pcap_addr *a;
30
+    int i = 0;
31
+    int no;
32
+
33
+    if (pcap_findalldevs(&alldevs, errbuf) < 0) {
34
+        printf("pcap_findalldevs error\n");
35
+        return 1;
36
+    }
37
+
38
+    for (d=alldevs; d; d=d->next) {
39
+        printf("%d :  %s\n", ++i, (d->description)?(d->description):(d->name));
40
+    }
41
+
42
+    printf("number : ");
43
+    scanf("%d", &no);
44
+
45
+    if (!(no > 0 && no <= i)) {
46
+        printf("number error\n");
47
+        return 1;
48
+    }
49
+
50
+    for (d=alldevs, i=0; d; d=d->next) {
51
+        if (no == ++i)  break;
52
+    }
53
+
54
+    if (!(adhandle= pcap_open_live(d->name, 65536, 1, 1000, errbuf))) {
55
+        printf("pcap_open_live error %s\n", d->name);
56
+        pcap_freealldevs(alldevs);
57
+        return -1;
58
+    }
59
+
60
+    pcap_freealldevs(alldevs);
61
+
62
+    pcap_loop(adhandle, 0, packet_handler, NULL);
63
+
64
+    pcap_close(adhandle);
65
+
66
+    return 0;
67
+}
68
+