Changeset 384f081 in mainline
- Timestamp:
- 2024-04-07T09:32:59Z (8 months ago)
- Children:
- 6c1e7c0
- Parents:
- 8d9217d
- Location:
- uspace
- Files:
-
- 3 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; -
uspace/lib/pcap/include/pcapctl_dump.h
r8d9217d r384f081 49 49 } pcapctl_sess_t; 50 50 51 extern errno_t pcapctl_dump_open( const char *svcname, pcapctl_sess_t **rsess);51 extern errno_t pcapctl_dump_open(int *, pcapctl_sess_t **rsess); 52 52 extern errno_t pcapctl_dump_close(pcapctl_sess_t *sess); 53 53 extern errno_t pcapctl_dump_start(const char *, pcapctl_sess_t *); 54 54 extern errno_t pcapctl_dump_stop(pcapctl_sess_t *); 55 55 extern errno_t pcapctl_list(void); 56 extern errno_t pcapctl_is_valid_device(int *); 56 57 57 58 #endif -
uspace/lib/pcap/src/pcapctl_dump.c
r8d9217d r384f081 43 43 #include "pcapdump_iface.h" 44 44 45 //static service_id_t *pcap_svcs = NULL; ??46 47 static errno_t str2num(const char *str, size_t *number)48 {49 size_t num = 0;50 if (*str == 0)51 return ELIMIT;52 if (!isdigit(*str))53 return EINVAL;54 while (isdigit(*str)) {55 num = num * 10 + ((*str) - '0');56 str++;57 }58 59 *number = num;60 return EOK;61 }62 45 /** Finish an async exchange on the pcapctl session 63 46 * … … 69 52 } 70 53 71 static errno_t pcapctl_cat_get_svc( const char *drv_name, service_id_t *svc)54 static errno_t pcapctl_cat_get_svc(int *index, service_id_t *svc) 72 55 { 73 56 errno_t rc; … … 85 68 if (rc != EOK) { 86 69 printf("Error resolving list of pcap services.\n"); 87 return rc; 88 } 89 90 for (unsigned i = 0; i < count; ++i) { 91 char *name = NULL; 92 loc_service_get_name(pcap_svcs[i], &name); 93 if (!str_cmp(drv_name, name)) { 94 *svc = pcap_svcs[i]; 95 return EOK; 96 } 97 } 98 free(pcap_svcs); 99 return 1; 100 } 101 70 free(pcap_svcs); 71 return rc; 72 } 73 if (*index < (int)count) { 74 *svc = pcap_svcs[*index]; 75 free(pcap_svcs); 76 return EOK; 77 } 78 79 return ENOENT; 80 } 81 82 errno_t pcapctl_is_valid_device(int *index) 83 { 84 errno_t rc; 85 category_id_t pcap_cat; 86 size_t count; 87 service_id_t *pcap_svcs = NULL; 88 89 rc = loc_category_get_id("pcap", &pcap_cat, 0); 90 if (rc != EOK) { 91 printf("Error resolving category pcap.\n"); 92 return rc; 93 } 94 95 rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count); 96 if (rc != EOK) { 97 printf("Error resolving list of pcap services.\n"); 98 free(pcap_svcs); 99 return rc; 100 } 101 if (*index + 1 > (int)count || *index < 0) { 102 return EINVAL; 103 } 104 return EOK; 105 } 106 107 /** 108 * 109 */ 102 110 errno_t pcapctl_list(void) 103 111 { … … 130 138 } 131 139 132 static errno_t pcapctl_get_name_from_number(const char *svcnum, const char **svcname)133 {134 errno_t rc;135 category_id_t pcap_cat;136 size_t count;137 service_id_t *pcap_svcs = NULL;138 139 rc = loc_category_get_id("pcap", &pcap_cat, 0);140 if (rc != EOK) {141 printf("Error resolving category pcap.\n");142 return rc;143 }144 size_t num;145 rc = str2num(svcnum, &num);146 if (rc != EOK) {147 printf("Error converting char* to size_t.\n");148 free(pcap_svcs);149 return rc;150 }151 152 rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count);153 if (rc != EOK) {154 printf("Error resolving list of pcap services.\n");155 free(pcap_svcs);156 return rc;157 }158 159 if (num >= count) {160 printf("Error finding device: no device with such number\n");161 free(pcap_svcs);162 return EINVAL;163 }164 char *name = NULL;165 rc = loc_service_get_name(pcap_svcs[num], &name);166 if (rc != EOK) {167 printf("Error resolving name");168 }169 170 *svcname = name;171 printf("%s\n", *svcname);172 return EOK;173 }174 175 140 /** 176 141 * 177 142 */ 178 errno_t pcapctl_dump_open( const char *svcnum, pcapctl_sess_t **rsess)143 errno_t pcapctl_dump_open(int *index, pcapctl_sess_t **rsess) 179 144 { 180 145 errno_t rc; … … 184 149 return ENOMEM; 185 150 186 const char *svcname; 187 188 rc = pcapctl_get_name_from_number(svcnum, &svcname); 189 if (rc != EOK) { 190 return rc; 191 } 192 193 rc = pcapctl_cat_get_svc(svcname, &svc); 194 if (rc != EOK) { 151 rc = pcapctl_cat_get_svc(index, &svc); 152 if (rc != EOK) { 153 printf("Error finding the device with index: %d\n", *index); 195 154 goto error; 196 155 } 156 197 157 async_sess_t *new_session = loc_service_connect(svc, INTERFACE_PCAP_CONTROL, 0); 198 158 if (new_session == NULL) {
Note:
See TracChangeset
for help on using the changeset viewer.