Browse code

이더넷 프레임 출력 함수 구현

Kang IkSeon authored on22/03/2018 09:02:09
Showing3 changed files

... ...
@@ -16,11 +16,11 @@
16 16
 #include <pcap.h>
17 17
 
18 18
 #include "src/lib.h"
19
+#include "src/ethernet.h"
19 20
 
20 21
 void packet_handler(u_char *param,
21 22
   const struct pcap_pkthdr *header, const u_char *pkt_data) {
22
-  printf("caplen : %d\n", header->caplen);
23
-  printf("len : %d\n", header->len);
23
+  dump_ethernet_header(pkt_data);
24 24
 }
25 25
 
26 26
 int main(int argc, char **argv) {
... ...
@@ -7,4 +7,35 @@
7 7
  *     Copyright:  Copyright (c) 2018, HiSEON
8 8
  */
9 9
 
10
+#include <stdio.h>
11
+#include <arpa/inet.h>
12
+#include <net/ethernet.h>
13
+
10 14
 #include "src/ethernet.h"
15
+
16
+void dump_ethernet_header(const u_char *pkt_data) {
17
+  struct ether_header *header = (struct ether_header *)pkt_data;
18
+
19
+  const char *name = NULL;
20
+
21
+  u_int8_t *dmac = header->ether_dhost;
22
+  u_int8_t *smac = header->ether_shost;
23
+  u_int16_t type = ntohs(header->ether_type);
24
+
25
+  switch (type) {
26
+    case ETHERTYPE_IP:
27
+      name = "IP";
28
+      break;
29
+    case ETHERTYPE_ARP:
30
+      name = "ARP";
31
+      break;
32
+    default:
33
+      name = "Unknwon";
34
+      break;
35
+  }
36
+
37
+  printf("%02x:%02x:%02x:%02x:%02x:%02x => " \
38
+      "%02x:%02x:%02x:%02x:%02x:%02x (%s) \n",
39
+  smac[0], smac[1], smac[2], smac[3], smac[4], smac[5],
40
+  dmac[0], dmac[1], dmac[2], dmac[3], dmac[4], dmac[5], name);
41
+}
... ...
@@ -10,6 +10,8 @@
10 10
 #ifndef SRC_ETHERNET_H_
11 11
 #define SRC_ETHERNET_H_
12 12
 
13
+#include <sys/types.h>
13 14
 
15
+void dump_ethernet_header(const u_char *pkt_data);
14 16
 
15 17
 #endif  // SRC_ETHERNET_H_