Changes in uspace/app/pcapctl/main.c [e4cc266:6c1e7c0] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/pcapctl/main.c
re4cc266 r6c1e7c0 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" 40 42 41 43 #define NAME "pcapctl" 44 #define DEFAULT_DEV_NUM 0 42 45 43 pcapctl_sess_t* sess; 44 45 static errno_t start_dumping(const char *svc_name, const char *name) 46 static errno_t start_dumping(int *dev_number, const char *name) 46 47 { 47 errno_t rc = pcapctl_dump_open(svc_name, &sess); 48 pcapctl_sess_t *sess = NULL; 49 errno_t rc = pcapctl_dump_open(dev_number, &sess); 48 50 if (rc != EOK) { 49 51 return 1; 50 52 } 53 51 54 pcapctl_dump_start(name, sess); 52 55 pcapctl_dump_close(sess); … … 54 57 } 55 58 56 /** Session might */ 57 static errno_t stop_dumping(const char *svc_name) 59 static errno_t stop_dumping(int *dev_number) 58 60 { 59 errno_t rc = pcapctl_dump_open(svc_name, &sess); 61 pcapctl_sess_t *sess = NULL; 62 errno_t rc = pcapctl_dump_open(dev_number, &sess); 60 63 if (rc != EOK) { 61 64 return 1; 62 65 } 63 64 66 pcapctl_dump_stop(sess); 65 67 pcapctl_dump_close(sess); … … 67 69 } 68 70 69 static void list_devs(void) { 71 static void list_devs(void) 72 { 70 73 pcapctl_list(); 71 74 } 72 75 73 static void usage(const char *progname) 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 89 static void usage(void) 74 90 { 75 fprintf(stderr, "Usage:\n"); 76 fprintf(stderr, " %s list: List of devices\n", progname); 77 fprintf(stderr, " %s start <device> <outfile>: Packets dumped from <device> will be written to <outfile>\n", progname); 78 fprintf(stderr, " %s stop <device>: Dumping from <device> stops\n", progname); 91 printf("Usage:\n" 92 NAME " --list | -l \n" 93 "\tList of devices\n" 94 NAME " --start | -r --device= | -d <device number from list> --outfile= | -f <outfile>\n" 95 "\tPackets dumped from device will be written to <outfile>\n" 96 NAME " --stop | -t --device= | -d <device>\n" 97 "\tDumping from <device> stops\n" 98 NAME " --start | -s --outfile= | -f <outfile>\n" 99 "\tPackets dumped from the 0. device from the list will be written to <outfile>\n" 100 NAME " --help | -h\n" 101 "\tShow this application help.\n"); 79 102 } 80 103 81 104 int main(int argc, char *argv[]) 82 105 { 83 if (argc < 2) { 84 usage(argv[0]); 85 return 1; 86 } else { 87 if (str_cmp(argv[1], "--help") == 0 || str_cmp(argv[1], "-h") == 0) { 88 usage(argv[0]); 89 return 0; 90 } else if (str_cmp(argv[1], "list") == 0) { 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) { 113 usage(); 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': 91 130 list_devs(); 92 131 return 0; 93 } else if (str_cmp(argv[1], "start") == 0) { 94 if (argc != 4) { 95 usage(argv[0]); 96 return 1; 97 } 98 start_dumping(argv[2], argv[3]); 99 } else if (str_cmp(argv[1], "stop") == 0) { 100 if (argc != 3) { 101 usage(argv[0]); 102 return 1; 103 } 104 stop_dumping(argv[2]); 105 fprintf(stdout, "Dumping was stopped\n"); 106 return EOK; 107 } else { 108 usage(argv[0]); 109 return 1; 132 case 'h': 133 usage(); 134 return 0; 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; 110 144 } 145 } 146 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); 111 155 } 112 156 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.