source: mainline/uspace/app/pcapcat/main.c@ 01ccd702

Last change on this file since 01ccd702 was 01ccd702, checked in by Nataliia Korop <n.corop08@…>, 10 months ago

pcapcat dumps file hdr

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2 * Copyright (c) 2024 Nataliia Korop
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdint.h>
30#include <stdio.h>
31#include <time.h>
32#include <stdbool.h>
33#include <errno.h>
34#include <str.h>
35#include <io/log.h>
36#include "pcap.h"
37
38#define NAME "pcapcat"
39
40typedef struct {
41 uint32_t linktype;
42 void (*parse)(const char *);
43} linktype_parser_t;
44
45
46
47static void read_head(FILE *file)
48{
49 pcap_file_header_t data;
50 memset(&data, 0, sizeof(pcap_file_header_t));
51
52 size_t bytesRead = fread(&data, 1, sizeof(pcap_file_header_t), file);
53 if (bytesRead < sizeof(pcap_file_header_t)) {
54 printf("Error: Could not read enough bytes (read %zu bytes)\n", bytesRead);
55 fclose(file);
56 return;
57 }
58
59 printf("LinkType: %d\n", data.additional);
60 printf("Magic number: 0x%x\n", data.magic_number);
61 return;
62}
63
64static void eth_parse(const char *file_path)
65{
66 FILE *f = fopen(file_path, "rb");
67 if (f == NULL){
68 printf("File %s does not exist.\n", file_path);
69 return;
70 }
71
72 read_head(f);
73
74 pcap_packet_header_t hdr;
75
76 memset(&hdr, 0, sizeof(pcap_packet_header_t));
77 size_t read_bytes = fread(&hdr, 1, sizeof(pcap_packet_header_t), f);
78
79 while (read_bytes > 0)
80 {
81 if (read_bytes < sizeof(pcap_packet_header_t)) {
82 printf("Error: Could not read enough bytes (read %zu bytes)\n", read_bytes);
83 return;
84 }
85 printf("0x%x: %d, %d\n", hdr.magic_stamp, hdr.captured_length, hdr.original_length);
86
87 fread(NULL, 1, (size_t)hdr.captured_length, f);
88 printf("After\n");
89 read_bytes = fread(&hdr, 1, sizeof(pcap_packet_header_t), f);
90 }
91
92 fclose(f);
93}
94
95static const linktype_parser_t eth_parser = {
96 .parse = &eth_parse,
97 .linktype = PCAP_LINKTYPE_ETHERNET
98};
99
100
101int main(int argc, char *argv[])
102{
103 if (argc != 2)
104 {
105 return 1;
106 }
107
108 eth_parser.parse(argv[1]);
109
110 return 0;
111}
Note: See TracBrowser for help on using the repository browser.