Changeset adbd7e1 in mainline
- Timestamp:
- 2025-10-11T18:23:30Z (2 months ago)
- Children:
- aefdccd
- Parents:
- 503ce85
- Location:
- uspace/app
- Files:
-
- 4 edited
-
pcapcat/eth_parser.c (modified) (7 diffs)
-
pcapcat/linktype_parser.h (modified) (1 diff)
-
pcapcat/main.c (modified) (2 diffs)
-
pcapctl/main.c (modified) (2 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 -
uspace/app/pcapcat/linktype_parser.h
r503ce85 radbd7e1 39 39 40 40 typedef 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); 44 44 } linktype_parser_t; 45 45 -
uspace/app/pcapcat/main.c
r503ce85 radbd7e1 27 27 */ 28 28 29 30 29 #include <stdint.h> 31 30 #include <stdio.h> … … 45 44 46 45 static const linktype_parser_t eth_parser = { 47 .parse_packets = ð_parse_frames,48 .parse_file_header = ð_parse_header,49 .linktype = PCAP_LINKTYPE_ETHERNET46 .parse_packets = ð_parse_frames, 47 .parse_file_header = ð_parse_header, 48 .linktype = PCAP_LINKTYPE_ETHERNET 50 49 }; 51 50 52 static const linktype_parser_t parsers[1] = { eth_parser};51 static const linktype_parser_t parsers[1] = { eth_parser }; 53 52 54 53 static int parse_file(const char *file_path, int packet_count, bool verbose_flag) 55 54 { 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 } 61 60 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)); 64 63 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 } 71 70 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 } 80 79 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 } 85 84 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); 88 87 89 fclose(f);90 return 0;88 fclose(f); 89 return 0; 91 90 } 92 91 93 92 static void usage() 94 93 { 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"); 105 103 } 106 104 107 105 static 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 } 111 109 }; 112 113 110 114 111 int main(int argc, char *argv[]) 115 112 { 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 } 126 122 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 } 144 139 145 if (optind < argc) {146 filename = argv[optind];147 }140 if (optind < argc) { 141 filename = argv[optind]; 142 } 148 143 149 int ret_val = parse_file(filename, count, verbose);144 int ret_val = parse_file(filename, count, verbose); 150 145 151 return ret_val;146 return ret_val; 152 147 } -
uspace/app/pcapctl/main.c
r503ce85 radbd7e1 116 116 { "new", required_argument, 0, 'N' }, /* file name as argument */ 117 117 { "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) */ 119 119 { "device", required_argument, 0, 'd' }, 120 120 { "list", no_argument, 0, 'l' }, … … 148 148 NAME " --list | -l \n" 149 149 "\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" 156 156 NAME " --start | -r --device= | -d <device number from list> --outfile= | -o <outfile> --ops= | p <ops index>\n" 157 157 "\tPackets dumped from device will be written to <outfile>\n"
Note:
See TracChangeset
for help on using the changeset viewer.
