Changeset 384f081 in mainline for uspace/app/pcapctl/main.c
- Timestamp:
- 2024-04-07T09:32:59Z (7 months ago)
- Children:
- 6c1e7c0
- Parents:
- 8d9217d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/pcapctl/main.c
r8d9217d r384f081 36 36 #include <str.h> 37 37 #include <errno.h> 38 #include <arg_parse.h> 39 #include <getopt.h> 38 40 39 41 #include "pcapctl_dump.h" … … 42 44 #define DEFAULT_DEV_NUM 0 43 45 44 //pcapctl_sess_t* sess = NULL; 45 46 static errno_t start_dumping(const char *svc_name, const char *name) 46 static errno_t start_dumping(int *dev_number, const char *name) 47 47 { 48 48 pcapctl_sess_t *sess = NULL; 49 errno_t rc = pcapctl_dump_open( svc_name, &sess);49 errno_t rc = pcapctl_dump_open(dev_number, &sess); 50 50 if (rc != EOK) { 51 51 return 1; … … 57 57 } 58 58 59 /** Session might */ 60 static errno_t stop_dumping(const char *svc_name) 59 static errno_t stop_dumping(int *dev_number) 61 60 { 62 61 pcapctl_sess_t *sess = NULL; 63 errno_t rc = pcapctl_dump_open( svc_name, &sess);62 errno_t rc = pcapctl_dump_open(dev_number, &sess); 64 63 if (rc != EOK) { 65 64 return 1; … … 75 74 } 76 75 76 /** 77 * Array of supported commandline options 78 */ 79 static const struct option opts[] = { 80 { "device", required_argument, 0, 'd' }, 81 { "list", no_argument, 0, 'l' }, 82 { "help", no_argument, 0, 'h' }, 83 { "outfile", required_argument, 0, 'f' }, 84 { "start", no_argument, 0, 'r' }, 85 { "stop", no_argument, 0, 't' }, 86 { 0, 0, 0, 0 } 87 }; 88 77 89 static void usage(void) 78 90 { 79 91 printf("Usage:\n" 80 NAME " list\n"92 NAME " --list | -l \n" 81 93 "\tList of devices\n" 82 NAME " start --device= | -d <device number from list> <outfile>\n"94 NAME " start --device= | -d <device number from list> --outfile | -f <outfile>\n" 83 95 "\tPackets dumped from device will be written to <outfile>\n" 84 96 NAME " stop --device= | -d <device>\n" … … 92 104 int main(int argc, char *argv[]) 93 105 { 94 if (argc < 2) { 106 bool start = false; 107 bool stop = false; 108 int dev_number = DEFAULT_DEV_NUM; 109 const char *output_file_name; 110 int idx = 0; 111 int ret = 0; 112 if (argc == 1) { 95 113 usage(); 96 return 1; 97 } else { 98 /** help */ 99 if (str_cmp(argv[1], "--help") == 0 || str_cmp(argv[1], "-h") == 0) { 114 return 0; 115 } 116 while (ret != -1) { 117 ret = getopt_long(argc, argv, "d:lhf:rt", opts, &idx); 118 switch (ret) { 119 case 'd': 120 char *rest; 121 long result = strtol(optarg, &rest, 10); 122 dev_number = (int)result; 123 errno_t rc = pcapctl_is_valid_device(&dev_number); 124 if (rc != EOK) { 125 printf("Device with index %d not found\n", dev_number); 126 return 1; 127 } 128 break; 129 case 'l': 130 list_devs(); 131 return 0; 132 case 'h': 100 133 usage(); 101 134 return 0; 102 /** list */ 103 } else if (str_cmp(argv[1], "list") == 0) { 104 list_devs(); 105 return 0; 106 /** start with/out devnum */ 107 } else if (str_cmp(argv[1], "start") == 0) { 108 if (argc == 3) { 109 start_dumping((char *)"0", argv[2]); 110 return 0; 111 } else if (argc == 4) { 112 start_dumping(argv[2], argv[3]); 113 return 0; 114 } else { 115 usage(); 116 return 1; 117 } 118 /** Stop with/out devnum */ 119 } else if (str_cmp(argv[1], "stop") == 0) { 120 if (argc == 2) { 121 stop_dumping((char *)"0"); 122 fprintf(stdout, "Dumping was stopped\n"); 123 return 0; 124 } else if (argc == 3) { 135 case 'f': 136 output_file_name = optarg; 137 break; 138 case 'r': 139 start = true; 140 break; 141 case 't': 142 stop = true; 143 break; 144 } 145 } 125 146 126 stop_dumping(argv[2]); 127 fprintf(stdout, "Dumping was stopped\n"); 128 } else { 129 usage(); 130 return 1; 131 } 132 } else { 133 usage(); 134 return 1; 135 } 147 printf("%s: HelenOS Packet Dumping utility: device - %d\n", NAME, dev_number); 148 149 if (start) { 150 // start with dev number and optional..name 151 start_dumping(&dev_number, output_file_name); 152 } else if (stop) { 153 //stop with dev number 154 stop_dumping(&dev_number); 136 155 } 137 156 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.