Changeset adbd7e1 in mainline for uspace/app/pcapcat/main.c


Ignore:
Timestamp:
2025-10-11T18:23:30Z (4 months ago)
Author:
Nataliia Korop <n.corop08@…>
Children:
aefdccd
Parents:
503ce85
Message:

ticket/packet-capture: ccheck

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/pcapcat/main.c

    r503ce85 radbd7e1  
    2727 */
    2828
    29 
    3029#include <stdint.h>
    3130#include <stdio.h>
     
    4544
    4645static const linktype_parser_t eth_parser = {
    47     .parse_packets = &eth_parse_frames,
    48     .parse_file_header = &eth_parse_header,
    49     .linktype = PCAP_LINKTYPE_ETHERNET
     46        .parse_packets = &eth_parse_frames,
     47        .parse_file_header = &eth_parse_header,
     48        .linktype = PCAP_LINKTYPE_ETHERNET
    5049};
    5150
    52 static const linktype_parser_t parsers[1] = {eth_parser};
     51static const linktype_parser_t parsers[1] = { eth_parser };
    5352
    5453static int parse_file(const char *file_path, int packet_count, bool verbose_flag)
    5554{
    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     }
     55        FILE *f = fopen(file_path, "rb");
     56        if (f == NULL) {
     57                printf("File %s does not exist.\n", file_path);
     58                return 1;
     59        }
    6160
    62     pcap_file_header_t hdr;
    63     memset(&hdr, 0, sizeof(pcap_file_header_t));
     61        pcap_file_header_t hdr;
     62        memset(&hdr, 0, sizeof(pcap_file_header_t));
    6463
    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     }
     64        size_t bytes_read = fread(&hdr, 1, sizeof(pcap_file_header_t), f);
     65        if (bytes_read < sizeof(pcap_file_header_t)) {
     66                printf("Error: Could not read enough bytes (read %zu bytes)\n", bytes_read);
     67                fclose(f);
     68                return 1;
     69        }
    7170
    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     }
     71        int parser_count = sizeof(parsers) / sizeof(linktype_parser_t);
     72        int parser_index = -1;
     73        for (int i = 0; i < parser_count; ++i) {
     74                if (parsers[i].linktype == hdr.additional) {
     75                        parser_index = i;
     76                        break;
     77                }
     78        }
    8079
    81     if (parser_index == -1) {
    82         printf("There is no parser for Linktype %d.\n", hdr.additional);
    83         return 1;
    84     }
     80        if (parser_index == -1) {
     81                printf("There is no parser for Linktype %d.\n", hdr.additional);
     82                return 1;
     83        }
    8584
    86     parsers[parser_index].parse_file_header(&hdr);
    87     parsers[parser_index].parse_packets(f, packet_count, verbose_flag);
     85        parsers[parser_index].parse_file_header(&hdr);
     86        parsers[parser_index].parse_packets(f, packet_count, verbose_flag);
    8887
    89     fclose(f);
    90     return 0;
     88        fclose(f);
     89        return 0;
    9190}
    9291
    9392static void usage()
    9493{
    95     printf("HelenOS cat utility for PCAP file format.\n"
    96     "Can run during dumping process.\n"
    97     "Usage:\n"
    98     NAME " <filename>\n"
    99     "\tPrint all packets from file <filename>.\n"
    100     NAME " --count= | -c <number> <filename>\n"
    101     "\tPrint first <number> packets from <filename>.\n"
    102     NAME " --verbose | -v <filename>\n"
    103     "\tPrint verbose description (with TCP ports) of packets.\n"
    104     );
     94        printf("HelenOS cat utility for PCAP file format.\n"
     95            "Can run during dumping process.\n"
     96            "Usage:\n"
     97            NAME " <filename>\n"
     98            "\tPrint all packets from file <filename>.\n"
     99            NAME " --count= | -c <number> <filename>\n"
     100            "\tPrint first <number> packets from <filename>.\n"
     101            NAME " --verbose | -v <filename>\n"
     102            "\tPrint verbose description (with TCP ports) of packets.\n");
    105103}
    106104
    107105static struct option options[] = {
    108     {"count", required_argument, 0, 'c'},
    109     {"verbose", no_argument, 0, 'v'},
    110     {0, 0, 0, 0}
     106        { "count", required_argument, 0, 'c' },
     107        { "verbose", no_argument, 0, 'v' },
     108        { 0, 0, 0, 0 }
    111109};
    112 
    113110
    114111int main(int argc, char *argv[])
    115112{
    116     int ret = 0;
    117     int idx = 0;
    118     int count = -1;
    119     bool verbose = false;
    120     const char *filename = "";
    121     if (argc == 1)
    122     {
    123         usage();
    124         return 0;
    125     }
     113        int ret = 0;
     114        int idx = 0;
     115        int count = -1;
     116        bool verbose = false;
     117        const char *filename = "";
     118        if (argc == 1) {
     119                usage();
     120                return 0;
     121        }
    126122
    127     while (ret != -1) {
    128         ret = getopt_long(argc, argv, "c:v", options, &idx);
    129         switch (ret)
    130         {
    131         case 'c':
    132             count = atoi(optarg);
    133             break;
    134         case 'v':
    135             verbose = true;
    136             break;
    137         case '?':
    138             printf("Unknown option or missing argument.\n");
    139             return 1;
    140         default:
    141             break;
    142         }
    143     }
     123        while (ret != -1) {
     124                ret = getopt_long(argc, argv, "c:v", options, &idx);
     125                switch (ret) {
     126                case 'c':
     127                        count = atoi(optarg);
     128                        break;
     129                case 'v':
     130                        verbose = true;
     131                        break;
     132                case '?':
     133                        printf("Unknown option or missing argument.\n");
     134                        return 1;
     135                default:
     136                        break;
     137                }
     138        }
    144139
    145     if (optind < argc) {
    146         filename = argv[optind];
    147     }
     140        if (optind < argc) {
     141                filename = argv[optind];
     142        }
    148143
    149     int ret_val = parse_file(filename, count, verbose);
     144        int ret_val = parse_file(filename, count, verbose);
    150145
    151     return ret_val;
     146        return ret_val;
    152147}
Note: See TracChangeset for help on using the changeset viewer.