Changeset fb31682 in mainline


Ignore:
Timestamp:
2024-12-13T08:44:05Z (10 months ago)
Author:
Nataliia Korop <n.corop08@…>
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)
Message:

user friendly options, trying to start while dumping → err msg

Location:
uspace
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/pcapctl/main.c

    r28ed2d89 rfb31682  
    4444#define NAME "pcapctl"
    4545#define DEFAULT_DEV_NUM 0
    46 #define DEFAULT_OPS_NUM 0
    4746#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
    4852
    4953static errno_t start_dumping(int *dev_number, const char *name, int *ops_index)
     
    5660
    5761        rc = pcapctl_is_valid_ops_number(ops_index, sess);
    58         if (rc != EOK)
    59         {
     62        if (rc != EOK) {
    6063                printf("Wrong number of ops: %d.\n", *ops_index);
    6164                pcapctl_dump_close(sess);
     
    6568        rc = pcapctl_dump_start(name, ops_index, sess);
    6669        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                }
    6773                printf("Starting the dumping was not successful.\n");
    6874        }
     
    95101 */
    96102static 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' }, //??
    97107        { "device", required_argument, 0, 'd' },
    98108        { "list", no_argument, 0, 'l' },
     
    102112        { "stop", no_argument, 0, 't' },
    103113        { "ops", required_argument, 0, 'p' },
    104         { "force", no_argument, 0, 'f'},
     114        { "force", no_argument, 0, 'f' },
    105115        { 0, 0, 0, 0 }
    106116};
     
    109119{
    110120        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        }
    116126}
    117127
     
    121131            NAME " --list | -l \n"
    122132            "\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"
    124134            "\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"
    126136            "\tDumping from <device> stops\n"
    127             NAME " --start | -s --outfile= | -f <outfile>\n"
     137            NAME " --start | -r --outfile= | -o <outfile>\n"
    128138            "\tPackets dumped from the 0. device from the list will be written to <outfile>\n"
    129139            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");
    131143}
    132144
     
    136148        bool stop = false;
    137149        int dev_number = DEFAULT_DEV_NUM;
    138         int ops_number = DEFAULT_OPS_NUM;
     150        int ops_number = DEFAULT_FILE_OPS;
    139151        bool forced = false;
    140152        const char *output_file_name = "";
     
    146158        }
    147159        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);
    149161                switch (ret) {
    150162                case 'd':
     
    158170                        }
    159171                        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;
    160189                case 'l':
    161190                        list_devs();
     
    174203                        break;
    175204                case 'p':
    176                         char* ops_inval;
     205                        char *ops_inval;
    177206                        long ops_result = strtol(optarg, &ops_inval, DECIMAL_SYSTEM);
    178207                        ops_number = (int)ops_result;
     
    184213        }
    185214
    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);
    187221
    188222        if (start) {
    189223
    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);
    193226                        return 0;
    194227                }
     
    201234        } else {
    202235                usage();
     236                return 1;
    203237        }
    204238        return 0;
  • uspace/lib/pcap/include/pcap_dumper.h

    r28ed2d89 rfb31682  
    4646} pcap_dumper_t;
    4747
     48extern errno_t pcap_dumper_start(pcap_dumper_t *, const char *);
    4849extern void pcap_dumper_stop(pcap_dumper_t *);
    4950extern int pcap_dumper_get_ops_number(void);
    5051extern errno_t pcap_dumper_set_ops(pcap_dumper_t *, int);
    51 extern errno_t pcap_dumper_start(pcap_dumper_t *, const char *);
    5252extern void pcap_dumper_add_packet(pcap_dumper_t *, const void *data, size_t size);
    5353
  • uspace/lib/pcap/include/pcapdump_client.h

    r28ed2d89 rfb31682  
    5656extern errno_t pcapctl_list(void);
    5757extern errno_t pcapctl_is_valid_device(int *);
    58 extern errno_t pcapctl_is_valid_ops_number(int *index, pcapctl_sess_t* sess);
    59 
     58extern errno_t pcapctl_is_valid_ops_number(int *, pcapctl_sess_t *);
    6059
    6160#endif
  • uspace/lib/pcap/src/pcap.c

    r28ed2d89 rfb31682  
    5959{
    6060        uint32_t magic_version = PCAP_MAGIC_MICRO;
    61         if (nano)
    62         {
     61        if (nano) {
    6362                magic_version = PCAP_MAGIC_NANO;
    6463        }
  • uspace/lib/pcap/src/pcap_dumper.c

    r28ed2d89 rfb31682  
    3939#include "pcap_dumper.h"
    4040
    41 #define SHORT_OPS_BYTE_COUNT 60
     41#define SHORT_OPS_BYTE_COUNT 0x3C
    4242
    4343/** Initialize writing to .pcap file.
     
    5050static errno_t pcap_writer_to_file_init(pcap_writer_t *writer, const char *filename)
    5151{
     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
    5259        writer->data = fopen(filename, "a");
    5360        if (writer->data == NULL) {
     
    7178static errno_t pcap_writer_to_file_usb_init(pcap_writer_t *writer, const char *filename)
    7279{
     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
    7387        writer->data = fopen(filename, "a");
    7488        if (writer->data == NULL) {
     
    140154};
    141155
    142 static pcap_writer_ops_t ops[4] = {file_ops, short_file_ops, append_file_ops, usb_file_ops};
     156static pcap_writer_ops_t ops[4] = { file_ops, short_file_ops, append_file_ops, usb_file_ops };
    143157
    144158int pcap_dumper_get_ops_number(void)
     
    150164{
    151165        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         }
    157166
    158167        errno_t rc = dumper->writer.ops->open(&dumper->writer, name);
     
    201210}
    202211
    203 
    204 
    205212/** @}
    206213 */
  • uspace/lib/pcap/src/pcapdump_client.c

    r28ed2d89 rfb31682  
    105105}
    106106
    107 
    108 errno_t pcapctl_is_valid_ops_number(int *index, pcapctl_sess_t* sess)
     107errno_t pcapctl_is_valid_ops_number(int *index, pcapctl_sess_t *sess)
    109108{
    110109        async_exch_t *exch = async_exchange_begin(sess->sess);
     
    122121
    123122        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) {
    126124                return EINVAL;
    127125        }
     
    250248}
    251249
    252 
    253250/** @}
    254251 */
  • uspace/lib/pcap/src/pcapdump_srv.c

    r28ed2d89 rfb31682  
    6060        assert(str_length(data) == size && "Data were damaged during transmission.\n");
    6161
     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
    6270        rc = pcap_dumper_set_ops(dumper, ops_index);
    63         if (rc != EOK)
    64         {
     71        if (rc != EOK) {
    6572                log_msg(LOG_DEFAULT, LVL_DEBUG, "Setting ops for dumper was not successful.\n");
    6673                free(data);
     
    8289        async_answer_0(icall, EOK);
    8390}
    84 
    8591
    8692static void pcapdump_get_ops_num_srv(ipc_call_t *icall)
     
    128134}
    129135
    130 
    131 
    132136/** @}
    133137 */
Note: See TracChangeset for help on using the changeset viewer.