forked from pticon/netconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
220 lines (174 loc) · 5.37 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "network.h"
#include "dhcp.h"
#include <string.h>
#include <getopt.h>
static const char *prgname = "netconfig";
typedef struct config
{
char *eth;
char *ip;
char *mask;
char *bcast;
char *gw;
char *ns;
int save:1,
dhcp:1,
all:1;
} config_t;
static const struct option long_options [] =
{
{"help", no_argument, NULL, 0},
{"dhcp", no_argument, NULL, 0},
{"eth", required_argument, NULL, 0},
{"ip", required_argument, NULL, 0},
{"mask", required_argument, NULL, 0},
{"bcast", required_argument, NULL, 0},
{"gw", required_argument, NULL, 0},
{"ns", required_argument, NULL, 0},
{"save", no_argument, NULL, 0},
{"csv", no_argument, NULL, 0},
{"all", no_argument, NULL, 0},
{0, 0, 0, 0},
};
static int csv_display(const struct ifreq *ifr, void *unused);
static int display(const struct ifreq *ifr, void *unused);
typedef int (* display_t)(const struct ifreq *ifr, void *unused);
static display_t display_func = &display;
static void usage(const char *prg)
{
fprintf(stderr, "Display or set network informations\n");
fprintf(stderr, "usage: %s [options] [interface]\n", prg);
fprintf(stderr, "options:\n");
fprintf(stderr, "\t--help |-h : display this and exit\n");
fprintf(stderr, "\t--dhcp |-d : dhcp mode\n");
fprintf(stderr, "\t--eth |-e <ethaddr> : set MAC address\n");
fprintf(stderr, "\t--ip |-i <ip> : set ip address\n");
fprintf(stderr, "\t--mask |-m <mask> : set ip mask address\n");
fprintf(stderr, "\t--bcast |-b <addr> : set broadcast address\n");
fprintf(stderr, "\t--gw |-g <gw> : set the default gateway\n");
fprintf(stderr, "\t--ns |-n <server> : set the name server\n");
fprintf(stderr, "\t--save |-s : save the configuration\n");
fprintf(stderr, "\t--csv |-c : output display as a CSV\n");
fprintf(stderr, "\t--all |-a : consider all of the interfaces\n");
}
static int parse_long_options(const char *opt)
{
if ( !strcmp(opt, "help") ||
!strcmp(opt, "dhcp") ||
!strcmp(opt, "eth") ||
!strcmp(opt, "ip") ||
!strcmp(opt, "mask") ||
!strcmp(opt, "bcast") ||
!strcmp(opt, "gw") ||
!strcmp(opt, "ns") ||
!strcmp(opt, "save") ||
!strcmp(opt, "csv") ||
!strcmp(opt, "all") ||
!strcmp(opt, "dhcp") )
return opt[0];
fprintf(stderr, "Unknown option --%s\n", opt);
return -1;
}
static int parse_options(int argc, char * const argv[], config_t *conf)
{
int c,
index;
memset(conf, 0, sizeof(config_t));
while ( (c = getopt_long(argc, argv, "hde:i:m:b:g:n:sca",
long_options, &index)) != -1 )
{
if ( c == 0 &&
(c = parse_long_options(long_options[index].name)) < 0 )
return -1;
switch ( c )
{
case 'h':
usage(prgname);
return 0;
case 'd':
conf->dhcp++;
break;
case 'e':
conf->eth = optarg;
break;
case 'i':
conf->ip = optarg;
break;
case 'm':
conf->mask = optarg;
break;
case 'b':
conf->bcast = optarg;
break;
case 'g':
conf->gw = optarg;
break;
case 'n':
conf->ns = optarg;
break;
case 's':
conf->save++;
break;
case 'c':
display_func = &csv_display;
break;
case 'a':
conf->all++;
break;
default:
fprintf(stderr, "unknow option -%c\n", c);
return -1;
}
}
return 1;
}
static int csv_display(const struct ifreq *ifr, void *unused)
{
char str[NI_MAXHOST];
static int header = 0;
if ( !header )
{
printf("if,plug,dyn,mac,ip,mask,bcast,gw,ns\n");;
header++;
}
printf("%s,", ifr->ifr_name);
printf("%d,", isInterfacePlugged(ifr) ? 1 : 0);
printf("%d,", isInterfaceDynamic(ifr->ifr_name) ?
1 : 0);
if ( getMacAddress(ifr, str, sizeof(str)) == 0 )
printf("%s", str);
printf(",");
if ( getIpAddress(ifr, str, sizeof(str)) == 0 )
printf("%s", str);
printf(",");
if ( getIpMask(ifr, str, sizeof(str)) == 0 )
printf("%s", str);
printf(",");
if ( getIpBroadcast(ifr, str, sizeof(str)) == 0 )
printf("%s", str);
printf(",");
if ( getIpGateway(ifr, str, sizeof(str)) == 0 )
printf("%s", str);
printf(",");
if ( getDomainNameServer(str, sizeof(str)) == 0 )
printf("%s", str);
printf("\n");
return 0;
}
static int display(const struct ifreq *ifr, void *unused)
{
saveInterfaceIpConfig(ifr, MANUAL);
return 0;
}
int main(int argc, char *argv[])
{
int ret = -1;
if ( networkInit() )
{
fprintf(stderr, "Cannot init\n");
return -1;
}
addAllInterfaces();
ret = foreachInterfaceIpv4(display_func, NULL);
return ret;
}