| 2 | 5 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,45 @@ |
| 1 |
+/*! |
|
| 2 |
+ * @file dumpcode.c |
|
| 3 |
+ * @author Kang IkSeon (ikseon1026@gmail.com) |
|
| 4 |
+ * |
|
| 5 |
+ * @internal |
|
| 6 |
+ * Created: 2018년 01월 30일 |
|
| 7 |
+ * Copyright: Copyright (c) 2018, Kang IkSeon |
|
| 8 |
+ */ |
|
| 9 |
+ |
|
| 10 |
+#include <stdio.h> |
|
| 11 |
+#include <ctype.h> |
|
| 12 |
+ |
|
| 13 |
+#include "src/dumpcode.h" |
|
| 14 |
+ |
|
| 15 |
+void printchar(unsigned char c) {
|
|
| 16 |
+ if (isprint(c)) |
|
| 17 |
+ printf("%c", c);
|
|
| 18 |
+ else |
|
| 19 |
+ printf(".");
|
|
| 20 |
+} |
|
| 21 |
+ |
|
| 22 |
+void dumpcode(void *ptr, int len) {
|
|
| 23 |
+ int i; |
|
| 24 |
+ for (i=0; i < len; i++) {
|
|
| 25 |
+ if (i%16 == 0) |
|
| 26 |
+ printf("%p ", ((unsigned char *)ptr + i));
|
|
| 27 |
+ printf("%02x ", ((unsigned char *)ptr)[i]);
|
|
| 28 |
+ if (i%16-15 == 0) {
|
|
| 29 |
+ int j; |
|
| 30 |
+ printf(" ");
|
|
| 31 |
+ for (j=i-15; j <= i; j++) |
|
| 32 |
+ printchar(((unsigned char *)ptr)[j]); |
|
| 33 |
+ printf("\n");
|
|
| 34 |
+ } |
|
| 35 |
+ } |
|
| 36 |
+ if (i%16 != 0) {
|
|
| 37 |
+ int j; |
|
| 38 |
+ int spaces = (len-i+16-i%16)*3+2; |
|
| 39 |
+ for (j=0; j < spaces; j++) |
|
| 40 |
+ printf(" ");
|
|
| 41 |
+ for (j=i-i%16; j < len; j++) |
|
| 42 |
+ printchar(((unsigned char *)ptr)[j]); |
|
| 43 |
+ } |
|
| 44 |
+ printf("\n");
|
|
| 45 |
+} |
| 0 | 46 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,19 @@ |
| 1 |
+/*! |
|
| 2 |
+ * @file dumpcode.h |
|
| 3 |
+ * @author Kang IkSeon (ikseon1026@gmail.com) |
|
| 4 |
+ * |
|
| 5 |
+ * @internal |
|
| 6 |
+ * Created: 2018년 01월 30일 |
|
| 7 |
+ * Copyright: Copyright (c) 2018, Kang IkSeon |
|
| 8 |
+ */ |
|
| 9 |
+ |
|
| 10 |
+#ifndef SRC_DUMPCODE_H_ |
|
| 11 |
+#define SRC_DUMPCODE_H_ |
|
| 12 |
+ |
|
| 13 |
+/* |
|
| 14 |
+ * 오하라님께서 개발하신 dumpcode |
|
| 15 |
+ */ |
|
| 16 |
+void printchar(unsigned char c); |
|
| 17 |
+void dumpcode(void *ptr, int len); |
|
| 18 |
+ |
|
| 19 |
+#endif // SRC_DUMPCODE_H_ |
| 0 | 20 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,68 @@ |
| 1 |
+/*! |
|
| 2 |
+ * @file main.c |
|
| 3 |
+ * @author Kang IkSeon (ikseon1026@gmail.com) |
|
| 4 |
+ * |
|
| 5 |
+ * @internal |
|
| 6 |
+ * Created: 2018년 01월 30일 |
|
| 7 |
+ * Copyright: Copyright (c) 2018, Kang IkSeon |
|
| 8 |
+ */ |
|
| 9 |
+ |
|
| 10 |
+#include <stdio.h> |
|
| 11 |
+#include <stdint.h> |
|
| 12 |
+ |
|
| 13 |
+#include "src/dumpcode.h" |
|
| 14 |
+ |
|
| 15 |
+#define MAX 4096 |
|
| 16 |
+#define SAMPLE "data/sample.pcap" |
|
| 17 |
+ |
|
| 18 |
+typedef struct pcap_hdr_s {
|
|
| 19 |
+ uint32_t magic_number; /* magic number */ |
|
| 20 |
+ uint16_t version_major; /* major version number */ |
|
| 21 |
+ uint16_t version_minor; /* minor version number */ |
|
| 22 |
+ int32_t thiszone; /* GMT to local correction */ |
|
| 23 |
+ uint32_t sigfigs; /* accuracy of timestamps */ |
|
| 24 |
+ uint32_t snaplen; /* max length of captured packets, in octets */ |
|
| 25 |
+ uint32_t network; /* data link type */ |
|
| 26 |
+} pcap_hdr_t; |
|
| 27 |
+ |
|
| 28 |
+typedef struct pcaprec_hdr_s {
|
|
| 29 |
+ uint32_t ts_sec; /* timestamp seconds */ |
|
| 30 |
+ uint32_t ts_usec; /* timestamp microseconds */ |
|
| 31 |
+ uint32_t incl_len; /* number of octets of packet saved in file */ |
|
| 32 |
+ uint32_t orig_len; /* actual length of packet */ |
|
| 33 |
+} pcaprec_hdr_t; |
|
| 34 |
+ |
|
| 35 |
+int main() {
|
|
| 36 |
+ int n, len; |
|
| 37 |
+ char buffer[MAX]; |
|
| 38 |
+ |
|
| 39 |
+ pcap_hdr_t header; |
|
| 40 |
+ pcaprec_hdr_t packet; |
|
| 41 |
+ |
|
| 42 |
+ FILE *fp; |
|
| 43 |
+ |
|
| 44 |
+ if (!(fp= fopen(SAMPLE, "r"))) {
|
|
| 45 |
+ return 1; |
|
| 46 |
+ } |
|
| 47 |
+ |
|
| 48 |
+ n = fread(&header, 1, sizeof(header), fp); |
|
| 49 |
+ |
|
| 50 |
+ printf("version : %d.%d\n\n",
|
|
| 51 |
+ header.version_major, header.version_minor); |
|
| 52 |
+ |
|
| 53 |
+ while (1) {
|
|
| 54 |
+ if (fread(&packet, 1, sizeof(packet), fp) <= 0) break; |
|
| 55 |
+ len = packet.incl_len; |
|
| 56 |
+ |
|
| 57 |
+ if (fread(buffer, 1, len, fp) <= 0) break; |
|
| 58 |
+ |
|
| 59 |
+ printf("packet length : %d [%d.%d]\n",
|
|
| 60 |
+ packet.incl_len, packet.ts_sec, packet.ts_usec); |
|
| 61 |
+ dumpcode(buffer, len); |
|
| 62 |
+ printf("\n");
|
|
| 63 |
+ } |
|
| 64 |
+ |
|
| 65 |
+ fclose(fp); |
|
| 66 |
+ |
|
| 67 |
+ return 0; |
|
| 68 |
+} |