Changeset fb31682 in mainline
- Timestamp:
- 2024-12-13T08:44:05Z (10 months ago)
- Children:
- 87b490e3
- Parents:
- 28ed2d89
- git-author:
- Nataliia Korop <n.corop08@…> (2024-11-29 10:41:19)
- git-committer:
- Nataliia Korop <n.corop08@…> (2024-12-13 08:44:05)
- Location:
- uspace
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/pcapctl/main.c
r28ed2d89 rfb31682 44 44 #define NAME "pcapctl" 45 45 #define DEFAULT_DEV_NUM 0 46 #define DEFAULT_OPS_NUM 047 46 #define DECIMAL_SYSTEM 10 47 48 #define DEFAULT_FILE_OPS 0 49 #define SHORT_FILE_OPS 1 50 #define APPEND_FILE_OPS 2 51 #define USB_FILE_OPS 3 48 52 49 53 static errno_t start_dumping(int *dev_number, const char *name, int *ops_index) … … 56 60 57 61 rc = pcapctl_is_valid_ops_number(ops_index, sess); 58 if (rc != EOK) 59 { 62 if (rc != EOK) { 60 63 printf("Wrong number of ops: %d.\n", *ops_index); 61 64 pcapctl_dump_close(sess); … … 65 68 rc = pcapctl_dump_start(name, ops_index, sess); 66 69 if (rc != EOK) { 70 if (rc == EBUSY) { 71 printf("Dumping for device %d is in process, stop to start dumping to file %s.\n", *dev_number, name); 72 } 67 73 printf("Starting the dumping was not successful.\n"); 68 74 } … … 95 101 */ 96 102 static const struct option opts[] = { 103 { "append", required_argument, 0, 'A' }, // file as argument and ops 0 if not exist and 2 if exists 104 { "new", required_argument, 0, 'N' }, // file name as argument 105 { "truncated", required_argument, 0, 'T' }, // truncated ops 106 { "usb", required_argument, 0, 'U' }, //?? 97 107 { "device", required_argument, 0, 'd' }, 98 108 { "list", no_argument, 0, 'l' }, … … 102 112 { "stop", no_argument, 0, 't' }, 103 113 { "ops", required_argument, 0, 'p' }, 104 { "force", no_argument, 0, 'f' },114 { "force", no_argument, 0, 'f' }, 105 115 { 0, 0, 0, 0 } 106 116 }; … … 109 119 { 110 120 vfs_stat_t stats; 111 112 if (vfs_stat_path(path, &stats) != EOK) 113 return false; 114 115 return true; 121 if (vfs_stat_path(path, &stats) != EOK) { 122 return false; 123 } else { 124 return true; 125 } 116 126 } 117 127 … … 121 131 NAME " --list | -l \n" 122 132 "\tList of devices\n" 123 NAME " --start | -r --device= | -d <device number from list> --outfile= | - f <outfile>\n"133 NAME " --start | -r --device= | -d <device number from list> --outfile= | -o <outfile> --ops= | p <ops index>\n" 124 134 "\tPackets dumped from device will be written to <outfile>\n" 125 NAME " --stop | -t --device= | -d <device >\n"135 NAME " --stop | -t --device= | -d <device number from list>\n" 126 136 "\tDumping from <device> stops\n" 127 NAME " --start | - s --outfile= | -f<outfile>\n"137 NAME " --start | -r --outfile= | -o <outfile>\n" 128 138 "\tPackets dumped from the 0. device from the list will be written to <outfile>\n" 129 139 NAME " --help | -h\n" 130 "\tShow this application help.\n"); 140 "\tShow this application help.\n" 141 NAME " --force | -f" 142 "\tTo open existing file and write to it.\n"); 131 143 } 132 144 … … 136 148 bool stop = false; 137 149 int dev_number = DEFAULT_DEV_NUM; 138 int ops_number = DEFAULT_ OPS_NUM;150 int ops_number = DEFAULT_FILE_OPS; 139 151 bool forced = false; 140 152 const char *output_file_name = ""; … … 146 158 } 147 159 while (ret != -1) { 148 ret = getopt_long(argc, argv, " d:lho:rtp:f", opts, &idx);160 ret = getopt_long(argc, argv, "A:N:T:U:d:lho:rtp:f", opts, &idx); 149 161 switch (ret) { 150 162 case 'd': … … 158 170 } 159 171 break; 172 case 'A': 173 output_file_name = optarg; 174 if (file_exists(output_file_name)) { 175 ops_number = APPEND_FILE_OPS; 176 } 177 break; 178 case 'N': 179 output_file_name = optarg; 180 break; 181 case 'T': 182 output_file_name = optarg; 183 ops_number = SHORT_FILE_OPS; 184 break; 185 case 'U': 186 output_file_name = optarg; 187 ops_number = USB_FILE_OPS; 188 break; 160 189 case 'l': 161 190 list_devs(); … … 174 203 break; 175 204 case 'p': 176 char *ops_inval;205 char *ops_inval; 177 206 long ops_result = strtol(optarg, &ops_inval, DECIMAL_SYSTEM); 178 207 ops_number = (int)ops_result; … … 184 213 } 185 214 186 printf("%s: HelenOS Packet Dumping utility: device - %d.\n", NAME, dev_number); 215 if (!str_cmp(output_file_name, "") && start) { 216 printf("Dumping destination was not specified. Specify with --outfile | -o\n"); 217 return 1; 218 } 219 220 printf("%s: HelenOS Packet Dumping utility: device - %d, ops - %d.\n", NAME, dev_number, ops_number); 187 221 188 222 if (start) { 189 223 190 if (file_exists(output_file_name) && !forced) 191 { 192 printf("File %s already exists. If you want to write to it, then use flag --force.\n", output_file_name); 224 if (file_exists(output_file_name) && !forced && ops_number != 2) { 225 printf("File %s already exists. If you want to overwrite to it, then use flag --force.\n", output_file_name); 193 226 return 0; 194 227 } … … 201 234 } else { 202 235 usage(); 236 return 1; 203 237 } 204 238 return 0; -
uspace/lib/pcap/include/pcap_dumper.h
r28ed2d89 rfb31682 46 46 } pcap_dumper_t; 47 47 48 extern errno_t pcap_dumper_start(pcap_dumper_t *, const char *); 48 49 extern void pcap_dumper_stop(pcap_dumper_t *); 49 50 extern int pcap_dumper_get_ops_number(void); 50 51 extern errno_t pcap_dumper_set_ops(pcap_dumper_t *, int); 51 extern errno_t pcap_dumper_start(pcap_dumper_t *, const char *);52 52 extern void pcap_dumper_add_packet(pcap_dumper_t *, const void *data, size_t size); 53 53 -
uspace/lib/pcap/include/pcapdump_client.h
r28ed2d89 rfb31682 56 56 extern errno_t pcapctl_list(void); 57 57 extern errno_t pcapctl_is_valid_device(int *); 58 extern errno_t pcapctl_is_valid_ops_number(int *index, pcapctl_sess_t* sess); 59 58 extern errno_t pcapctl_is_valid_ops_number(int *, pcapctl_sess_t *); 60 59 61 60 #endif -
uspace/lib/pcap/src/pcap.c
r28ed2d89 rfb31682 59 59 { 60 60 uint32_t magic_version = PCAP_MAGIC_MICRO; 61 if (nano) 62 { 61 if (nano) { 63 62 magic_version = PCAP_MAGIC_NANO; 64 63 } -
uspace/lib/pcap/src/pcap_dumper.c
r28ed2d89 rfb31682 39 39 #include "pcap_dumper.h" 40 40 41 #define SHORT_OPS_BYTE_COUNT 6041 #define SHORT_OPS_BYTE_COUNT 0x3C 42 42 43 43 /** Initialize writing to .pcap file. … … 50 50 static errno_t pcap_writer_to_file_init(pcap_writer_t *writer, const char *filename) 51 51 { 52 /** For overwriting file if already exists. */ 53 writer->data = fopen(filename, "w"); 54 if (writer->data == NULL) { 55 return EINVAL; 56 } 57 fclose(writer->data); 58 52 59 writer->data = fopen(filename, "a"); 53 60 if (writer->data == NULL) { … … 71 78 static errno_t pcap_writer_to_file_usb_init(pcap_writer_t *writer, const char *filename) 72 79 { 80 /** For overwriting file if already exists. */ 81 writer->data = fopen(filename, "w"); 82 if (writer->data == NULL) { 83 return EINVAL; 84 } 85 fclose(writer->data); 86 73 87 writer->data = fopen(filename, "a"); 74 88 if (writer->data == NULL) { … … 140 154 }; 141 155 142 static pcap_writer_ops_t ops[4] = { file_ops, short_file_ops, append_file_ops, usb_file_ops};156 static pcap_writer_ops_t ops[4] = { file_ops, short_file_ops, append_file_ops, usb_file_ops }; 143 157 144 158 int pcap_dumper_get_ops_number(void) … … 150 164 { 151 165 fibril_mutex_lock(&dumper->mutex); 152 153 /** When try to start when already started, close current and starts new */154 if (dumper->to_dump) {155 pcap_dumper_stop(dumper);156 }157 166 158 167 errno_t rc = dumper->writer.ops->open(&dumper->writer, name); … … 201 210 } 202 211 203 204 205 212 /** @} 206 213 */ -
uspace/lib/pcap/src/pcapdump_client.c
r28ed2d89 rfb31682 105 105 } 106 106 107 108 errno_t pcapctl_is_valid_ops_number(int *index, pcapctl_sess_t* sess) 107 errno_t pcapctl_is_valid_ops_number(int *index, pcapctl_sess_t *sess) 109 108 { 110 109 async_exch_t *exch = async_exchange_begin(sess->sess); … … 122 121 123 122 int ops_count = (int)ipc_get_arg1(&answer); 124 if (*index + 1 > ops_count || *index < 0) 125 { 123 if (*index + 1 > ops_count || *index < 0) { 126 124 return EINVAL; 127 125 } … … 250 248 } 251 249 252 253 250 /** @} 254 251 */ -
uspace/lib/pcap/src/pcapdump_srv.c
r28ed2d89 rfb31682 60 60 assert(str_length(data) == size && "Data were damaged during transmission.\n"); 61 61 62 // Deadlock solution when trying to start dump while dumping (to the same device) 63 if (dumper->to_dump) { 64 free(data); 65 log_msg(LOG_DEFAULT, LVL_ERROR, "Trying to start dumping while dumping.\n"); 66 async_answer_0(icall, EBUSY); 67 return; 68 } 69 62 70 rc = pcap_dumper_set_ops(dumper, ops_index); 63 if (rc != EOK) 64 { 71 if (rc != EOK) { 65 72 log_msg(LOG_DEFAULT, LVL_DEBUG, "Setting ops for dumper was not successful.\n"); 66 73 free(data); … … 82 89 async_answer_0(icall, EOK); 83 90 } 84 85 91 86 92 static void pcapdump_get_ops_num_srv(ipc_call_t *icall) … … 128 134 } 129 135 130 131 132 136 /** @} 133 137 */
Note:
See TracChangeset
for help on using the changeset viewer.