[01ccd702] | 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 | */
|
---|
[3197b50] | 28 |
|
---|
[01ccd702] | 29 |
|
---|
| 30 | #include <stdint.h>
|
---|
| 31 | #include <stdio.h>
|
---|
[1ea8566] | 32 | #include <stdlib.h>
|
---|
[01ccd702] | 33 | #include <time.h>
|
---|
| 34 | #include <stdbool.h>
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <str.h>
|
---|
[3197b50] | 37 | #include <getopt.h>
|
---|
[01ccd702] | 38 | #include <io/log.h>
|
---|
[3197b50] | 39 | #include <pcap.h>
|
---|
| 40 |
|
---|
| 41 | #include "linktype_parser.h"
|
---|
| 42 | #include "eth_parser.h"
|
---|
[01ccd702] | 43 |
|
---|
| 44 | #define NAME "pcapcat"
|
---|
| 45 |
|
---|
[3197b50] | 46 | static const linktype_parser_t eth_parser = {
|
---|
| 47 | .parse_packets = ð_parse_packets,
|
---|
| 48 | .parse_file_header = ð_parse_header,
|
---|
| 49 | .linktype = PCAP_LINKTYPE_ETHERNET
|
---|
| 50 | };
|
---|
[01ccd702] | 51 |
|
---|
[3197b50] | 52 | static const linktype_parser_t parsers[1] = {eth_parser};
|
---|
[fc632e8] | 53 |
|
---|
[3197b50] | 54 | static int parse_file(const char *file_path, int packet_count, bool verbose_flag)
|
---|
[fc632e8] | 55 | {
|
---|
[3197b50] | 56 | FILE *f = fopen(file_path, "rb");
|
---|
| 57 | if (f == NULL){
|
---|
| 58 | printf("File %s does not exist.\n", file_path);
|
---|
| 59 | return 1;
|
---|
[fc632e8] | 60 | }
|
---|
| 61 |
|
---|
[3197b50] | 62 | pcap_file_header_t hdr;
|
---|
| 63 | memset(&hdr, 0, sizeof(pcap_file_header_t));
|
---|
[fc632e8] | 64 |
|
---|
[3197b50] | 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;
|
---|
[fc632e8] | 70 | }
|
---|
| 71 |
|
---|
[3197b50] | 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 | }
|
---|
[fc632e8] | 79 | }
|
---|
| 80 |
|
---|
[3197b50] | 81 | if (parser_index == -1) {
|
---|
| 82 | printf("There is no parser for Linktype %d.\n", hdr.additional);
|
---|
| 83 | return 1;
|
---|
| 84 | }
|
---|
[fc632e8] | 85 |
|
---|
[3197b50] | 86 | parsers[parser_index].parse_file_header(&hdr);
|
---|
| 87 | parsers[parser_index].parse_packets(f, packet_count, verbose_flag);
|
---|
| 88 | return 0;
|
---|
[fc632e8] | 89 | }
|
---|
| 90 |
|
---|
[3197b50] | 91 | static void usage()
|
---|
[fc632e8] | 92 | {
|
---|
[3197b50] | 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 | );
|
---|
[fc632e8] | 103 | }
|
---|
| 104 |
|
---|
[3197b50] | 105 | static struct option options[] = {
|
---|
| 106 | {"count", required_argument, 0, 'c'},
|
---|
| 107 | {"verbose", no_argument, 0, 'v'},
|
---|
| 108 | {0, 0, 0, 0}
|
---|
| 109 | };
|
---|
[01ccd702] | 110 |
|
---|
| 111 |
|
---|
[3197b50] | 112 | int main(int argc, char *argv[])
|
---|
[fc632e8] | 113 | {
|
---|
[3197b50] | 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 | }
|
---|
[fc632e8] | 124 |
|
---|
[3197b50] | 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);
|
---|
[fc632e8] | 131 | break;
|
---|
[3197b50] | 132 | case 'v':
|
---|
| 133 | verbose = true;
|
---|
[fc632e8] | 134 | break;
|
---|
[3197b50] | 135 | case '?':
|
---|
| 136 | printf("Unknown option or missing argument.\n");
|
---|
| 137 | return 1;
|
---|
[fc632e8] | 138 | default:
|
---|
| 139 | break;
|
---|
[01ccd702] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[3197b50] | 143 | if (optind < argc) {
|
---|
| 144 | filename = argv[optind];
|
---|
[01ccd702] | 145 | }
|
---|
| 146 |
|
---|
[3197b50] | 147 | int ret_val = parse_file(filename, count, verbose);
|
---|
[01ccd702] | 148 |
|
---|
[3197b50] | 149 | return ret_val;
|
---|
[01ccd702] | 150 | } |
---|