source: mainline/uspace/app/pcapcat/main.c@ 3197b50

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

pcapcat utility, without docs and with literals

  • Property mode set to 100644
File size: 4.3 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
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <time.h>
34#include <stdbool.h>
35#include <errno.h>
36#include <str.h>
37#include <getopt.h>
38#include <io/log.h>
39#include <pcap.h>
40
41#include "linktype_parser.h"
42#include "eth_parser.h"
43
44#define NAME "pcapcat"
45
46static const linktype_parser_t eth_parser = {
47 .parse_packets = &eth_parse_packets,
48 .parse_file_header = &eth_parse_header,
49 .linktype = PCAP_LINKTYPE_ETHERNET
50};
51
52static const linktype_parser_t parsers[1] = {eth_parser};
53
54static int parse_file(const char *file_path, int packet_count, bool verbose_flag)
55{
56 FILE *f = fopen(file_path, "rb");
57 if (f == NULL){
58 printf("File %s does not exist.\n", file_path);
59 return 1;
60 }
61
62 pcap_file_header_t hdr;
63 memset(&hdr, 0, sizeof(pcap_file_header_t));
64
65 size_t bytes_read = fread(&hdr, 1, sizeof(pcap_file_header_t), f);
66 if (bytes_read < sizeof(pcap_file_header_t)) {
67 printf("Error: Could not read enough bytes (read %zu bytes)\n", bytes_read);
68 fclose(f);
69 return 1;
70 }
71
72 int parser_count = sizeof(parsers) / sizeof(linktype_parser_t);
73 int parser_index = -1;
74 for (int i = 0; i < parser_count; ++i) {
75 if (parsers[i].linktype == hdr.additional) {
76 parser_index = i;
77 break;
78 }
79 }
80
81 if (parser_index == -1) {
82 printf("There is no parser for Linktype %d.\n", hdr.additional);
83 return 1;
84 }
85
86 parsers[parser_index].parse_file_header(&hdr);
87 parsers[parser_index].parse_packets(f, packet_count, verbose_flag);
88 return 0;
89}
90
91static void usage()
92{
93 printf("HelenOS cat utility for PCAP file format.\n"
94 "Can run during dumping process.\n"
95 "Usage:\n"
96 NAME " <filename>\n"
97 "\tPrint all packets from file <filename>.\n"
98 NAME " --count= | -c <number> <filename>\n"
99 "\tPrint first <number> packets from <filename>.\n"
100 NAME " --verbose | -v <filename>\n"
101 "\tPrint verbose description (with TCP ports) of packets.\n"
102 );
103}
104
105static struct option options[] = {
106 {"count", required_argument, 0, 'c'},
107 {"verbose", no_argument, 0, 'v'},
108 {0, 0, 0, 0}
109};
110
111
112int main(int argc, char *argv[])
113{
114 int ret = 0;
115 int idx = 0;
116 int count = -1;
117 bool verbose = false;
118 const char *filename = "";
119 if (argc == 1)
120 {
121 usage();
122 return 0;
123 }
124
125 while (ret != -1) {
126 ret = getopt_long(argc, argv, "c:v", options, &idx);
127 switch (ret)
128 {
129 case 'c':
130 count = atoi(optarg);
131 break;
132 case 'v':
133 verbose = true;
134 break;
135 case '?':
136 printf("Unknown option or missing argument.\n");
137 return 1;
138 default:
139 break;
140 }
141 }
142
143 if (optind < argc) {
144 filename = argv[optind];
145 }
146
147 int ret_val = parse_file(filename, count, verbose);
148
149 return ret_val;
150}
Note: See TracBrowser for help on using the repository browser.