Changeset adbd7e1 in mainline for uspace/app/pcapcat/main.c
- Timestamp:
- 2025-10-11T18:23:30Z (4 months ago)
- Children:
- aefdccd
- Parents:
- 503ce85
- File:
-
- 1 edited
-
uspace/app/pcapcat/main.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note:
See TracChangeset
for help on using the changeset viewer.
