-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_options.c
99 lines (79 loc) · 1.96 KB
/
cmd_options.c
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*parse cmd, getopts and put into vars(pcap_*) */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "def.h"
#include "cmd_options.h"
int pcap_type = Undetermined;
char pcap_filename[1024] = { 0 };
char pcap_intf[128] = { 0 };
int pcap_limit = 0;
char server_ip[128] = { 0 };
uint16_t server_port;
#define _(x) x
const char *usage =
"Usage:\n"
" " PROG_NAME " [ -f pcap_file | -i pcap_intf ] -s server_ip -p server_port { -c count }\n"
"\n"
"Examples:\n"
" " PROG_NAME " -f file.pcap -s 10.21.0.202 -p 80\n"
" " PROG_NAME " -i eth0 -s 10.21.0.202 -p 80 -c 10000\n";
static void print_version()
{
fprintf(stdout, "%s: %s\n\n", PROG_NAME, PROG_VERSION);
}
static void usage_exit(int status)
{
fprintf(stderr, "%s", usage);
exit(status);
}
void parse_cmd_options(int argc, const char **argv)
{
int sflag = 0;
int pflag = 0;
const char* options = "hvf:i:s:p:c:";
char cmd_opt;
while ((cmd_opt = getopt(argc, (char **)argv, options)) != -1) {
switch (cmd_opt) {
case 'f':
if (pcap_type == Online)
usage_exit(1);
strncpy(pcap_filename, optarg, sizeof(pcap_filename)-1);
pcap_type = Offline;
break;
case 'i':
if (pcap_type == Offline)
usage_exit(1);
strncpy(pcap_intf, optarg, sizeof(pcap_intf)-1);
pcap_type = Online;
break;
case 's':
strncpy(server_ip, optarg, sizeof(server_ip)-1);
sflag = 1;
break;
case 'p': //sscanf get its input from arg1, then cpy to arg3
if (sscanf(optarg, "%hu", &server_port) != 1) //%hu scans for an unsigned short and %hhu for an unsigned char .
usage_exit(1);
pflag = 1;
break;
case 'c':
if (sscanf(optarg, "%d", &pcap_limit) != 1)
usage_exit(1);
break;
case 'v':
print_version();
usage_exit(0);
break;
case 'h':
usage_exit(0);
break;
case '?':
default:
usage_exit(1);
break;
}
}
if (pcap_type == Undetermined || !sflag || !pflag)
usage_exit(1);
}