1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <getopt.h> #include <unistd.h>
static void parse_client_options(int argc, char *argv[]) { enum { OPT_SRCIP = 256, OPT_DSTIP, OPT_SRCPORT, OPT_DSTPORT };
static char *short_options = "hp:t:s:n:i:b:"; static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"srcip", required_argument, NULL, OPT_SRCIP }, {"dstip", required_argument, NULL, OPT_DSTIP }, {"srcport", required_argument, NULL, OPT_SRCPORT }, {"dstport", required_argument, NULL, OPT_DSTPORT }, {"proto", required_argument, NULL, 'p'}, {"tos", required_argument, NULL, 't'}, {"pktsize", required_argument, NULL, 's'}, {"interval", required_argument, NULL, 'i'}, {"bandwidth", required_argument, NULL, 'b'}, {"pktnums", required_argument, NULL, 'n'}, {0, 0, 0, 0 } };
int opt; while((opt = getopt_long_only( argc, argv, short_options, long_options, NULL)) != -1) {
switch(opt) { case 'h': client_help_info(); exit(EXIT_SUCCESS); case 'p': pkts.proto = atoi(optarg); if (pkts.proto != TCP && pkts.proto != UDP) { printf("参数错误: %d,只能为TCP(6)/UDP(17)\n", pkts.proto); exit(EXIT_FAILURE); } break; case 't': pkts.tos = atoi(optarg); break; case 's': pkts.pktsize = atoi(optarg); if (pkts.pktsize < 64 || pkts.pktsize > 1500) { printf("数据包大小应该在[64, 1500]的范围内\n"); exit(EXIT_FAILURE); } break; case 'n': pkts.pktnums = atoi(optarg); break; case 'i': pkts.interval = atoi(optarg); break; case 'b': pkts.bandwidth = atoi(optarg); break; case OPT_SRCIP: strncpy(pkts.srcip, optarg, 16); break; case OPT_DSTIP: strncpy(pkts.dstip, optarg, 16); break; case OPT_SRCPORT: pkts.srcport = atoi(optarg); break; case OPT_DSTPORT: pkts.dstport = atoi(optarg); break; default: client_help_info(); exit(EXIT_FAILURE); } } }
|