Changeset adbd7e1 in mainline for uspace/app/pcapcat/eth_parser.c
- Timestamp:
- 2025-10-11T18:23:30Z (4 months ago)
- Children:
- aefdccd
- Parents:
- 503ce85
- File:
-
- 1 edited
-
uspace/app/pcapcat/eth_parser.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/pcapcat/eth_parser.c
r503ce85 radbd7e1 93 93 static void read_from_buffer(unsigned char *buffer, size_t start_idx, size_t count, uint8_t *dst) 94 94 { 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 } 98 98 } 99 99 … … 104 104 static void parse_arp(unsigned char *buffer, size_t size) 105 105 { 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"); 125 125 } 126 126 … … 131 131 static void parse_tcp(unsigned char *buffer, size_t size) 132 132 { 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); 141 141 } 142 142 … … 148 148 static void parse_ip(unsigned char *buffer, size_t size, bool verbose) 149 149 { 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 } 177 177 } 178 178 … … 184 184 static void parse_eth_frame(void *data, size_t size, bool verbose_flag) 185 185 { 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 } 207 207 } 208 208 … … 212 212 void eth_parse_header(pcap_file_header_t *hdr) 213 213 { 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); 216 216 } 217 217 … … 223 223 void eth_parse_frames(FILE *pcap_file, int count, bool verbose_flag) 224 224 { 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 } 255 254 } 256 255
Note:
See TracChangeset
for help on using the changeset viewer.
