Changeset adbd7e1 in mainline


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

ticket/packet-capture: ccheck

Location:
uspace/app
Files:
4 edited

Legend:

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

    r503ce85 radbd7e1  
    9393static void read_from_buffer(unsigned char *buffer, size_t start_idx, size_t count, uint8_t *dst)
    9494{
    95     for (size_t i = start_idx; i < start_idx + count; ++i) {
    96         dst[i - start_idx] = buffer[i];
    97     }
     95        for (size_t i = start_idx; i < start_idx + count; ++i) {
     96                dst[i - start_idx] = buffer[i];
     97        }
    9898}
    9999
     
    104104static void parse_arp(unsigned char *buffer, size_t size)
    105105{
    106     if (size < ARP_TARGET_IP + IPV4_ADDR_SIZE) {
    107         printf("%s %s", ARP_TEXT, MALFORMED_PACKET);
    108         return;
    109     }
    110 
    111     uint8_t sender_mac[ETH_ADDR_SIZE];
    112     uint8_t sender_ip[IPV4_ADDR_SIZE];
    113     uint8_t target_mac[ETH_ADDR_SIZE];
    114     uint8_t target_ip[IPV4_ADDR_SIZE];
    115 
    116     read_from_buffer(buffer, ARP_SENDER_MAC, ETH_ADDR_SIZE, sender_mac);
    117     read_from_buffer(buffer, ARP_SENDER_IP, IPV4_ADDR_SIZE, sender_ip);
    118     read_from_buffer(buffer, ARP_TARGET_MAC, ETH_ADDR_SIZE, target_mac);
    119     read_from_buffer(buffer, ARP_TARGET_IP, IPV4_ADDR_SIZE, target_ip);
    120 
    121     PRINT_MAC("Sender", sender_mac, ", ");
    122     PRINT_IP("Sender", sender_ip, "  ");
    123     PRINT_MAC("Target", target_mac, ", ");
    124     PRINT_IP("Target", target_ip, "\n");
     106        if (size < ARP_TARGET_IP + IPV4_ADDR_SIZE) {
     107                printf("%s %s", ARP_TEXT, MALFORMED_PACKET);
     108                return;
     109        }
     110
     111        uint8_t sender_mac[ETH_ADDR_SIZE];
     112        uint8_t sender_ip[IPV4_ADDR_SIZE];
     113        uint8_t target_mac[ETH_ADDR_SIZE];
     114        uint8_t target_ip[IPV4_ADDR_SIZE];
     115
     116        read_from_buffer(buffer, ARP_SENDER_MAC, ETH_ADDR_SIZE, sender_mac);
     117        read_from_buffer(buffer, ARP_SENDER_IP, IPV4_ADDR_SIZE, sender_ip);
     118        read_from_buffer(buffer, ARP_TARGET_MAC, ETH_ADDR_SIZE, target_mac);
     119        read_from_buffer(buffer, ARP_TARGET_IP, IPV4_ADDR_SIZE, target_ip);
     120
     121        PRINT_MAC("Sender", sender_mac, ", ");
     122        PRINT_IP("Sender", sender_ip, "  ");
     123        PRINT_MAC("Target", target_mac, ", ");
     124        PRINT_IP("Target", target_ip, "\n");
    125125}
    126126
     
    131131static void parse_tcp(unsigned char *buffer, size_t size)
    132132{
    133     if (size < TCP_DST_PORT + TCP_PORT_SIZE) {
    134         printf("%s %s\n", TCP_TEXT, MALFORMED_PACKET);
    135         return;
    136     }
    137 
    138     uint16_t src_port = BIG_END_16(buffer, TCP_SRC_PORT);
    139     uint16_t dst_port = BIG_END_16(buffer, TCP_DST_PORT);
    140     printf("      [%s] source port: %d, destination port: %d\n", TCP_TEXT, src_port, dst_port);
     133        if (size < TCP_DST_PORT + TCP_PORT_SIZE) {
     134                printf("%s %s\n", TCP_TEXT, MALFORMED_PACKET);
     135                return;
     136        }
     137
     138        uint16_t src_port = BIG_END_16(buffer, TCP_SRC_PORT);
     139        uint16_t dst_port = BIG_END_16(buffer, TCP_DST_PORT);
     140        printf("      [%s] source port: %d, destination port: %d\n", TCP_TEXT, src_port, dst_port);
    141141}
    142142
     
    148148static void parse_ip(unsigned char *buffer, size_t size, bool verbose)
    149149{
    150     uint16_t total_length;
    151     uint8_t header_length;
    152     uint16_t payload_length;
    153     uint8_t ip_protocol;
    154     uint8_t src_ip[IPV4_ADDR_SIZE];
    155     uint8_t dst_ip[IPV4_ADDR_SIZE];
    156 
    157     if (size < IP_DST_ADDR + IPV4_ADDR_SIZE) {
    158         printf("%s %s", IP_TEXT, MALFORMED_PACKET);
    159         return;
    160     }
    161 
    162     header_length = (buffer[IP_HEADER_LEN] & LOWER_4_BITS) * HDR_SIZE_COEF;
    163     total_length = BIG_END_16(buffer, IP_TOTAL_LEN);
    164     payload_length = total_length - header_length;
    165     ip_protocol = buffer[IP_PROTOCOL];
    166 
    167     read_from_buffer(buffer, IP_SRC_ADDR, IPV4_ADDR_SIZE, src_ip);
    168     read_from_buffer(buffer, IP_DST_ADDR, IPV4_ADDR_SIZE, dst_ip);
    169 
    170     printf("%s header: %dB, payload: %dB, protocol: 0x%x, ", IP_TEXT, header_length, payload_length, ip_protocol);
    171     PRINT_IP("Source", src_ip, ", ");
    172     PRINT_IP("Destination", dst_ip, "\n");
    173 
    174     if (verbose && ip_protocol == IP_PROTOCOL_TCP) {
    175         parse_tcp(buffer, size);
    176     }
     150        uint16_t total_length;
     151        uint8_t header_length;
     152        uint16_t payload_length;
     153        uint8_t ip_protocol;
     154        uint8_t src_ip[IPV4_ADDR_SIZE];
     155        uint8_t dst_ip[IPV4_ADDR_SIZE];
     156
     157        if (size < IP_DST_ADDR + IPV4_ADDR_SIZE) {
     158                printf("%s %s", IP_TEXT, MALFORMED_PACKET);
     159                return;
     160        }
     161
     162        header_length = (buffer[IP_HEADER_LEN] & LOWER_4_BITS) * HDR_SIZE_COEF;
     163        total_length = BIG_END_16(buffer, IP_TOTAL_LEN);
     164        payload_length = total_length - header_length;
     165        ip_protocol = buffer[IP_PROTOCOL];
     166
     167        read_from_buffer(buffer, IP_SRC_ADDR, IPV4_ADDR_SIZE, src_ip);
     168        read_from_buffer(buffer, IP_DST_ADDR, IPV4_ADDR_SIZE, dst_ip);
     169
     170        printf("%s header: %dB, payload: %dB, protocol: 0x%x, ", IP_TEXT, header_length, payload_length, ip_protocol);
     171        PRINT_IP("Source", src_ip, ", ");
     172        PRINT_IP("Destination", dst_ip, "\n");
     173
     174        if (verbose && ip_protocol == IP_PROTOCOL_TCP) {
     175                parse_tcp(buffer, size);
     176        }
    177177}
    178178
     
    184184static void parse_eth_frame(void *data, size_t size, bool verbose_flag)
    185185{
    186     unsigned char* buffer = (unsigned char*)data;
    187 
    188     size_t eth_type_offset = 12;
    189     uint16_t protocol = BIG_END_16(buffer, eth_type_offset);
    190 
    191     switch (protocol){
    192         case ETHER_TYPE_ARP:
    193             printf("[%s] ", ARP_TEXT);
    194             parse_arp(buffer, size);
    195             break;
    196         case ETHER_TYPE_IP4:
    197             printf("[%s] ", IPV4_TEXT);
    198             parse_ip(buffer, size, verbose_flag);
    199             break;
    200         case ETHER_TYPE_IP6:
    201             printf("[%s]\n", IPV6_TEXT);
    202             break;
    203         default:
    204             printf("[0x%x]\n", protocol);
    205             break;
    206     }
     186        unsigned char *buffer = (unsigned char *)data;
     187
     188        size_t eth_type_offset = 12;
     189        uint16_t protocol = BIG_END_16(buffer, eth_type_offset);
     190
     191        switch (protocol) {
     192        case ETHER_TYPE_ARP:
     193                printf("[%s] ", ARP_TEXT);
     194                parse_arp(buffer, size);
     195                break;
     196        case ETHER_TYPE_IP4:
     197                printf("[%s] ", IPV4_TEXT);
     198                parse_ip(buffer, size, verbose_flag);
     199                break;
     200        case ETHER_TYPE_IP6:
     201                printf("[%s]\n", IPV6_TEXT);
     202                break;
     203        default:
     204                printf("[0x%x]\n", protocol);
     205                break;
     206        }
    207207}
    208208
     
    212212void eth_parse_header(pcap_file_header_t *hdr)
    213213{
    214     printf("LinkType: %d\n", hdr->additional);
    215     printf("Magic number:  0x%x\n", hdr->magic_number);
     214        printf("LinkType: %d\n", hdr->additional);
     215        printf("Magic number:  0x%x\n", hdr->magic_number);
    216216}
    217217
     
    223223void eth_parse_frames(FILE *pcap_file, int count, bool verbose_flag)
    224224{
    225     pcap_packet_header_t hdr;
    226 
    227     size_t read_bytes = fread(&hdr, 1, sizeof(pcap_packet_header_t), pcap_file);
    228     int packet_index = 1;
    229     while (read_bytes > 0)
    230     {
    231         if (read_bytes < sizeof(pcap_packet_header_t)) {
    232             printf("Error: Could not read enough bytes (read %zu bytes)\n", read_bytes);
    233             return;
    234         }
    235 
    236         printf("%04d) ", packet_index++);
    237 
    238         void *data = malloc(hdr.captured_length);
    239         read_bytes = fread(data, 1, (size_t)hdr.captured_length, pcap_file);
    240         if (read_bytes < (size_t)hdr.captured_length) {
    241             printf("Error: Could not read enough bytes (read %zu bytes)\n", read_bytes);
    242             return;
    243         }
    244         parse_eth_frame(data, (size_t)hdr.captured_length, verbose_flag);
    245         free(data);
    246 
    247         //Read first count packets from file.
    248         if (count != -1 && count == packet_index - 1) {
    249             return;
    250         }
    251 
    252         memset(&hdr, 0, sizeof(pcap_packet_header_t));
    253         read_bytes = fread(&hdr, 1, sizeof(pcap_packet_header_t), pcap_file);
    254     }
     225        pcap_packet_header_t hdr;
     226
     227        size_t read_bytes = fread(&hdr, 1, sizeof(pcap_packet_header_t), pcap_file);
     228        int packet_index = 1;
     229        while (read_bytes > 0) {
     230                if (read_bytes < sizeof(pcap_packet_header_t)) {
     231                        printf("Error: Could not read enough bytes (read %zu bytes)\n", read_bytes);
     232                        return;
     233                }
     234
     235                printf("%04d) ", packet_index++);
     236
     237                void *data = malloc(hdr.captured_length);
     238                read_bytes = fread(data, 1, (size_t)hdr.captured_length, pcap_file);
     239                if (read_bytes < (size_t)hdr.captured_length) {
     240                        printf("Error: Could not read enough bytes (read %zu bytes)\n", read_bytes);
     241                        return;
     242                }
     243                parse_eth_frame(data, (size_t)hdr.captured_length, verbose_flag);
     244                free(data);
     245
     246                //Read first count packets from file.
     247                if (count != -1 && count == packet_index - 1) {
     248                        return;
     249                }
     250
     251                memset(&hdr, 0, sizeof(pcap_packet_header_t));
     252                read_bytes = fread(&hdr, 1, sizeof(pcap_packet_header_t), pcap_file);
     253        }
    255254}
    256255
  • uspace/app/pcapcat/linktype_parser.h

    r503ce85 radbd7e1  
    3939
    4040typedef struct {
    41     uint32_t linktype;
    42     void (*parse_file_header)(pcap_file_header_t *);
    43     void (*parse_packets)(FILE *, int, bool);
     41        uint32_t linktype;
     42        void (*parse_file_header)(pcap_file_header_t *);
     43        void (*parse_packets)(FILE *, int, bool);
    4444} linktype_parser_t;
    4545
  • 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}
  • uspace/app/pcapctl/main.c

    r503ce85 radbd7e1  
    116116        { "new", required_argument, 0, 'N' }, /* file name as argument */
    117117        { "truncated", required_argument, 0, 'T' }, /* file as an argument with device 0 and dump truncated packets (for debugging purposes) */
    118         { "usb", required_argument, 0, 'U' }, /* todo: dump usb packets (not fully implemnted)*/
     118        { "usb", required_argument, 0, 'U' }, /* dump usb packets (not fully implemnted) */
    119119        { "device", required_argument, 0, 'd' },
    120120        { "list", no_argument, 0, 'l' },
     
    148148            NAME " --list | -l \n"
    149149            "\tList of devices\n"
    150                 NAME " --new= | -N <outfile>\n"
    151                 "\tStart dumping with ops - 0, on device - 0\n"
    152                 NAME " --append= | -A <outfile>\n"
    153                 "\tContinue dumping on device - 0 to already existing file\n"
    154                 NAME " --truncated= | -T <outfile>\n"
    155                 "\tStart dumping truncated packets to file on device - 0\n"
     150            NAME " --new= | -N <outfile>\n"
     151            "\tStart dumping with ops - 0, on device - 0\n"
     152            NAME " --append= | -A <outfile>\n"
     153            "\tContinue dumping on device - 0 to already existing file\n"
     154            NAME " --truncated= | -T <outfile>\n"
     155            "\tStart dumping truncated packets to file on device - 0\n"
    156156            NAME " --start | -r --device= | -d <device number from list> --outfile= | -o <outfile> --ops= | p <ops index>\n"
    157157            "\tPackets dumped from device will be written to <outfile>\n"
Note: See TracChangeset for help on using the changeset viewer.