-
Notifications
You must be signed in to change notification settings - Fork 36
/
args.c
160 lines (119 loc) · 3.07 KB
/
args.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <getopt.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "args.h"
#include "options.h"
void usage(char *msg, int exit_status)
{
fprintf(exit_status == 0 ? stdout : stderr,
"%s", USAGE_TXT);
if (msg) {
fprintf(exit_status == 0 ? stdout : stderr,
"\n%s\n", msg);
}
exit(exit_status);
}
void parse_args(int argc, char *argv[], address_pool *pool)
{
int c;
opterr = 0;
while ((c = getopt (argc, argv, "a:d:o:p:s:")) != -1)
switch (c) {
case 'a': // parse IP address pool
{
char *opt = strdup(optarg);
char *sfirst = opt;
char *slast = strchr(opt, ',');
if (slast == NULL)
usage("error: comma not present in option -a.", 1);
*slast = '\0';
slast++;
uint32_t *first, *last;
if (parse_ip(sfirst, (void **)&first) != 4)
usage("error: invalid first ip in address pool.", 1);
if (parse_ip(slast, (void **)&last) != 4)
usage("error: invalid last ip in address pool.", 1);
pool->indexes.first = *first;
pool->indexes.last = *last;
pool->indexes.current = *first;
free(first);
free(last);
free(opt);
break;
}
case 'd': // network device to use
{
strncpy(pool->device, optarg, sizeof(pool->device));
break;
}
case 'o': // parse dhcp option
{
uint8_t id;
char *opt = strdup(optarg);
char *name = opt;
char *value = strchr(opt, ',');
if (value == NULL)
usage("error: comma not present in option -o.", 1);
*value = '\0';
value++;
dhcp_option *option = calloc(1, sizeof(*option));
if((id = parse_option(option, name, value)) == 0) {
char msg[128];
snprintf(msg, sizeof(msg),
"error: invalid dhcp option specified: %s,%s",
name, value);
usage(msg, 1);
}
append_option(&pool->options, option);
if(option->id == IP_ADDRESS_LEASE_TIME)
pool->lease_time = ntohl(*((uint32_t *)option->data));
free(option);
free(opt);
break;
}
case 'p': // parse pending time
{
time_t *t;
if(parse_long(optarg, (void **)&t) != 4)
usage("error: invalid pending time.", 1);
pool->pending_time = *t;
free(t);
break;
}
case 's': // static binding
{
char *opt = strdup(optarg);
char *shw = opt;
char *sip = strchr(opt, ',');
if (sip == NULL)
usage("error: comma not present in option -s.", 1);
*sip = '\0';
sip++;
uint32_t *ip;
uint8_t *hw;
if (parse_mac(shw, (void **)&hw) != 6)
usage("error: invalid mac address in static binding.", 1);
if (parse_ip(sip, (void **)&ip) != 4)
usage("error: invalid ip in static binding.", 1);
add_binding(&pool->bindings, *ip, hw, 6, 1);
free(ip);
free(hw);
free(opt);
break;
}
case '?':
default:
usage(NULL, 1);
}
if(optind >= argc)
usage("error: server address not provided.", 1);
uint32_t *ip;
if (parse_ip(argv[optind], (void **)&ip) != 4)
usage("error: invalid server address.", 1);
pool->server_id = *ip;
free(ip);
}