Changeset 78edb5e in mainline
- Timestamp:
- 2024-03-19T15:15:50Z (10 months ago)
- Children:
- 8d9217d
- Parents:
- e4cc266
- Location:
- uspace
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/pcapctl/main.c
re4cc266 r78edb5e 41 41 #define NAME "pcapctl" 42 42 43 pcapctl_sess_t* sess;43 //pcapctl_sess_t* sess = NULL; 44 44 45 45 static errno_t start_dumping(const char *svc_name, const char *name) 46 46 { 47 pcapctl_sess_t* sess = NULL; 47 48 errno_t rc = pcapctl_dump_open(svc_name, &sess); 48 49 if (rc != EOK) { 49 50 return 1; 50 51 } 52 51 53 pcapctl_dump_start(name, sess); 52 54 pcapctl_dump_close(sess); … … 57 59 static errno_t stop_dumping(const char *svc_name) 58 60 { 61 pcapctl_sess_t* sess = NULL; 59 62 errno_t rc = pcapctl_dump_open(svc_name, &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); … … 71 73 } 72 74 73 static void usage( const char *progname)75 static void usage(void) 74 76 { 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); 77 printf("Usage:\n" 78 NAME " list \n" 79 "\tList of devices\n" 80 NAME " start --device= | -d <device number from list> <outfile>\n" 81 "\tPackets dumped from device will be written to <outfile>\n" 82 NAME " stop --device= | -d <device>\n" 83 "\tDumping from <device> stops\n" 84 NAME " start <outfile>\n" 85 "\tPackets dumped from the 1st device from the list will be written to <outfile>\n" 86 NAME " --help | -h\n" 87 "\tShow this application help.\n"); 79 88 } 80 89 … … 82 91 { 83 92 if (argc < 2) { 84 usage( argv[0]);93 usage(); 85 94 return 1; 86 95 } else { 96 /** help*/ 87 97 if (str_cmp(argv[1], "--help") == 0 || str_cmp(argv[1], "-h") == 0) { 88 usage( argv[0]);98 usage(); 89 99 return 0; 100 /** list*/ 90 101 } else if (str_cmp(argv[1], "list") == 0) { 91 102 list_devs(); 92 103 return 0; 104 /** start with/out devnum */ 93 105 } else if (str_cmp(argv[1], "start") == 0) { 94 if (argc != 4) { 95 usage(argv[0]); 106 if (argc == 3) { 107 start_dumping((char *)"0", argv[2]); 108 return 0; 109 } 110 else if (argc == 4) { 111 start_dumping(argv[2], argv[3]); 112 return 0; 113 } else { 114 usage(); 96 115 return 1; 97 116 } 98 start_dumping(argv[2], argv[3]);117 /** Stop with/out devnum */ 99 118 } else if (str_cmp(argv[1], "stop") == 0) { 100 if (argc != 3) { 101 usage(argv[0]); 119 if (argc == 2) { 120 stop_dumping((char *)"0"); 121 fprintf(stdout, "Dumping was stopped\n"); 122 return 0; 123 } 124 else if (argc == 3) { 125 126 stop_dumping(argv[2]); 127 fprintf(stdout, "Dumping was stopped\n"); 128 } else { 129 usage(); 102 130 return 1; 103 131 } 104 stop_dumping(argv[2]);105 fprintf(stdout, "Dumping was stopped\n");106 return EOK;107 132 } else { 108 usage( argv[0]);133 usage(); 109 134 return 1; 110 135 } -
uspace/lib/pcap/src/pcapctl_dump.c
re4cc266 r78edb5e 38 38 #include <str.h> 39 39 #include <stdlib.h> 40 40 #include <stdio.h> 41 #include <ctype.h> 41 42 #include "pcapctl_dump.h" 42 43 #include "pcapdump_iface.h" 43 44 45 //static service_id_t *pcap_svcs = NULL; ?? 46 47 static errno_t str2num(const char* str, size_t* number) { 48 size_t num = 0; 49 if (*str == 0) 50 return ELIMIT; 51 if (!isdigit(*str)) 52 return EINVAL; 53 while (isdigit(*str)) { 54 num = num * 10 + ((*str) - '0'); 55 str++; 56 } 57 58 *number = num; 59 return EOK; 60 } 44 61 /** Finish an async exchange on the pcapctl session 45 62 * … … 105 122 char *name = NULL; 106 123 loc_service_get_name(pcap_svcs[i], &name); 107 fprintf(stdout, " service: %s\n", name);124 fprintf(stdout, "%d. %s\n", i, name); 108 125 } 109 126 free(pcap_svcs); … … 112 129 113 130 114 errno_t pcapctl_dump_open(const char *svcname, pcapctl_sess_t **rsess) 131 static errno_t pcapctl_get_name_from_number(const char* svcnum, const char** svcname) { 132 133 errno_t rc; 134 category_id_t pcap_cat; 135 size_t count; 136 service_id_t *pcap_svcs = NULL; 137 138 rc = loc_category_get_id("pcap", &pcap_cat, 0); 139 if (rc != EOK) { 140 printf("Error resolving category pcap.\n"); 141 return rc; 142 } 143 size_t num; 144 rc = str2num(svcnum, &num); 145 if (rc != EOK) { 146 printf("Error converting char* to size_t.\n"); 147 free(pcap_svcs); 148 return rc; 149 } 150 151 rc = loc_category_get_svcs(pcap_cat, &pcap_svcs, &count); 152 if (rc != EOK) { 153 printf("Error resolving list of pcap services.\n"); 154 free(pcap_svcs); 155 return rc; 156 } 157 158 if (num >= count) { 159 printf("Error finding device: no device with such number\n"); 160 free(pcap_svcs); 161 return EINVAL; 162 } 163 char *name = NULL; 164 rc = loc_service_get_name(pcap_svcs[num], &name); 165 if (rc != EOK) { 166 printf("Error resolving name"); 167 } 168 169 *svcname = name; 170 printf("%s\n", *svcname); 171 return EOK; 172 } 173 174 /** 175 * 176 */ 177 errno_t pcapctl_dump_open(const char *svcnum, pcapctl_sess_t **rsess) 115 178 { 116 179 errno_t rc; … … 119 182 if (sess == NULL) 120 183 return ENOMEM; 184 185 186 const char* svcname; 187 188 rc = pcapctl_get_name_from_number(svcnum, &svcname); 189 if (rc != EOK) { 190 return rc; 191 } 121 192 122 193 rc = pcapctl_cat_get_svc(svcname, &svc); … … 130 201 goto error; 131 202 } 132 133 203 sess->sess = new_session; 134 204 *rsess = sess; … … 139 209 } 140 210 211 /** 212 * 213 */ 141 214 errno_t pcapctl_dump_close(pcapctl_sess_t *sess) 142 215 {
Note:
See TracChangeset
for help on using the changeset viewer.