Changeset 46e2152 in mainline for uspace/app/pcapcat/eth_parser.c


Ignore:
Timestamp:
2024-12-28T18:44:18Z (14 months ago)
Author:
Nataliia Korop <n.corop08@…>
Children:
503ce85
Parents:
caac052
Message:

pcapcat: typos, small fixes, pcapctl: comments, docs

File:
1 edited

Legend:

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

    rcaac052 r46e2152  
    6969#define BIG_END_16(buffer, idx) buffer[idx] << BYTE_SIZE | buffer[idx + 1]
    7070
     71/** Offsets of interesting fields in packet. */
     72
     73#define ARP_SENDER_MAC      22
     74#define ARP_SENDER_IP       28
     75#define ARP_TARGET_MAC      32
     76#define ARP_TARGET_IP       38
     77
     78#define TCP_SRC_PORT        34
     79#define TCP_DST_PORT        36
     80
     81#define IP_HEADER_LEN       14
     82#define IP_TOTAL_LEN        16
     83#define IP_PROTOCOL         23
     84#define IP_SRC_ADDR         26
     85#define IP_DST_ADDR         30
     86
    7187/** Read count bytes from char buffer.
    7288 *  @param buffer       of bytes to read from.
     
    88104static void parse_arp(unsigned char *buffer, size_t size)
    89105{
    90     size_t sender_mac_offset = 22;
    91     size_t sender_ip_offset = 28;
    92     size_t target_mac_offset = 32;
    93     size_t target_ip_offset = 38;
    94     if (size < target_ip_offset + IPV4_ADDR_SIZE) {
     106    if (size < ARP_TARGET_IP + IPV4_ADDR_SIZE) {
    95107        printf("%s %s", ARP_TEXT, MALFORMED_PACKET);
    96108        return;
     
    102114    uint8_t target_ip[IPV4_ADDR_SIZE];
    103115
    104     read_from_buffer(buffer, sender_mac_offset, ETH_ADDR_SIZE, sender_mac);
    105     read_from_buffer(buffer, sender_ip_offset, IPV4_ADDR_SIZE, sender_ip);
    106     read_from_buffer(buffer, target_mac_offset, ETH_ADDR_SIZE, target_mac);
    107     read_from_buffer(buffer, target_ip_offset, IPV4_ADDR_SIZE, target_ip);
     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);
    108120
    109121    PRINT_MAC("Sender", sender_mac, ", ");
     
    119131static void parse_tcp(unsigned char *buffer, size_t size)
    120132{
    121     size_t src_port_offset = 34;
    122     size_t dst_port_offset = 36;
    123 
    124     if (size < dst_port_offset + TCP_PORT_SIZE) {
     133    if (size < TCP_DST_PORT + TCP_PORT_SIZE) {
    125134        printf("%s %s\n", TCP_TEXT, MALFORMED_PACKET);
    126135        return;
    127136    }
    128137
    129     uint16_t src_port = BIG_END_16(buffer, src_port_offset);
    130     uint16_t dst_port = BIG_END_16(buffer, dst_port_offset);
     138    uint16_t src_port = BIG_END_16(buffer, TCP_SRC_PORT);
     139    uint16_t dst_port = BIG_END_16(buffer, TCP_DST_PORT);
    131140    printf("      [%s] source port: %d, destination port: %d\n", TCP_TEXT, src_port, dst_port);
    132141}
     
    146155    uint8_t dst_ip[IPV4_ADDR_SIZE];
    147156
    148     size_t hdr_length_offset = 14;
    149     size_t total_len_offset = 16;
    150     size_t protocol_offset = 23;
    151     size_t src_ip_offset = 26;
    152     size_t dst_ip_offset = 30;
    153 
    154     if (size < dst_ip_offset + IPV4_ADDR_SIZE) {
     157    if (size < IP_DST_ADDR + IPV4_ADDR_SIZE) {
    155158        printf("%s %s", IP_TEXT, MALFORMED_PACKET);
    156159        return;
    157160    }
    158161
    159     header_length = (buffer[hdr_length_offset] & LOWER_4_BITS) * HDR_SIZE_COEF;
    160     total_length = BIG_END_16(buffer, total_len_offset);
     162    header_length = (buffer[IP_HEADER_LEN] & LOWER_4_BITS) * HDR_SIZE_COEF;
     163    total_length = BIG_END_16(buffer, IP_TOTAL_LEN);
    161164    payload_length = total_length - header_length;
    162     ip_protocol = buffer[protocol_offset];
    163 
    164     read_from_buffer(buffer, src_ip_offset, IPV4_ADDR_SIZE, src_ip);
    165     read_from_buffer(buffer, dst_ip_offset, IPV4_ADDR_SIZE, dst_ip);
     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);
    166169
    167170    printf("%s header: %dB, payload: %dB, protocol: 0x%x, ", IP_TEXT, header_length, payload_length, ip_protocol);
     
    250253        read_bytes = fread(&hdr, 1, sizeof(pcap_packet_header_t), pcap_file);
    251254    }
    252 
    253     fclose(pcap_file);
    254255}
    255256
Note: See TracChangeset for help on using the changeset viewer.