-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvipif.c
172 lines (144 loc) · 3.64 KB
/
vipif.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
/* system includes */
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* ifupdown includes */
#include "ifupdown.h"
/* prototypes */
void help (const char *);
void usage (const char *);
void version (const char *, const char *);
#define VERSION "0.1"
void
help (const char *prog)
{
const char *fmt =
"Usage: %s <options> IFACE ADDR\n"
"\n"
"\t-h, --help\t\tthis help\n"
"\t-V, --version\t\tcopyright and version information\n"
"\t-i, --interfaces FILE\tuse FILE for interface definitions\n";
printf (fmt, prog);
exit (0);
}
void
usage (const char *prog)
{
char *fmt =
"%s: Use --help for help\n";
printf (fmt, prog);
exit (1);
}
void
version (const char *prog, const char *version)
{
const char *fmt =
"%s version %s\n"
"Copyright (c) 2011 Jeroen Koekkoek\n"
"\n"
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or (at\n"
"your option) any later version.\n";
printf (fmt, prog, version);
exit (0);
}
int
main (int argc, char *argv[])
{
char *prog;
char *interfaces = NULL;
char *ptr, *tail;
char *ucarp_name;
char *virt_addr, virt_name[80];
int c;
int cnt, optno;
int res = 0;
interfaces_file *iface_file = NULL;
interface_defn *iface, *ifaces, *ucarp_iface;
struct option long_opts[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{"interfaces", required_argument, NULL, 'i'},
{0, 0, 0, 0}
};
if ((prog = strrchr (argv[0], '/')))
prog++;
else
prog = argv[0];
for (;;) {
if ((c = getopt_long (argc, argv, "i:hV", long_opts, NULL)) == EOF)
break;
switch (c) {
case 'h':
help (prog);
break; /* never reached */
case 'V':
version (prog, VERSION);
break; /* never reached */
case 'i':
interfaces = strdup (optarg);
break;
default:
usage (prog);
break; /* never reached */
}
}
if ((optind + 2) > argc)
usage (prog);
if (! interfaces)
interfaces = strdup (INTERFACES);
/* parse interfaces file */
if (! (iface_file = read_interfaces (interfaces))) {
fprintf (stderr, "%s: cannot read interfaces file \"%s\"\n",
prog, interfaces);
exit (1);
}
ucarp_iface = NULL;
ucarp_name = argv[optind];
virt_addr = argv[optind + 1];
/* find interface ucarp binds to */
ifaces = iface_file->ifaces;
for (iface = ifaces; ! ucarp_iface && iface; iface = iface->next) {
if (strcmp (iface->logical_iface, ucarp_name) == 0)
ucarp_iface = iface;
}
ptr = NULL;
tail = NULL;
for (optno = 0; ! ptr && optno < ucarp_iface->n_options; optno++) {
if (strncmp (ucarp_iface->option[optno].name, "ucarp-vip", 9) == 0 &&
strcmp (ucarp_iface->option[optno].value, virt_addr) == 0)
ptr = ucarp_iface->option[optno].name;
}
if (! ptr)
goto cleanup;
memset (virt_name, '\0', 80);
if ((tail = strrchr (ptr, ':'))) {
for (ptr = ++tail; isdigit (*ptr); ptr++)
/* do noting */ ;
if (*ptr)
tail = NULL;
}
if (tail)
cnt = snprintf (virt_name, 79, "%s:ucarp:%s", ucarp_name, tail);
else
cnt = snprintf (virt_name, 79, "%s:ucarp", ucarp_name);
if (cnt < 0 || cnt > 79) {
fprintf (stderr, "%s: snprintf: %s\n", prog, strerror (errno));
goto error;
}
printf ("%s\n", virt_name);
res = 0;
cleanup:
if (interfaces)
free (interfaces);
if (iface_file)
free_interfaces (iface_file);
return res;
error:
res = 1;
goto cleanup;
}