]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/misc-progs/setaliases.c
a541a4fd20b418b0629b9515c9875455d5dac9e5
2 * setaliases - configure red aliased interfaces
4 * This program is distributed under the terms of the GNU General Public
5 * Licence. See the file COPYING for details.
7 * (c) Steve Bootes, 2002/04/15
9 * 21/04/03 Robert Kerr Changed to link directly to libsmooth rather than
10 * using a copy & paste
12 * $Id: setaliases.c,v 1.2.2.5 2006/07/25 23:15:20 franck78 Exp $
20 #include <sys/types.h>
24 #include "libsmooth.h"
28 struct keyvalue
*kv
= NULL
;
33 void exithandler(void)
35 if (kv
) freekeyvalues(kv
);
36 if (file
) fclose(file
);
42 char command
[STRING_SIZE
];
43 char red_netmask
[STRING_SIZE
];
44 char red_dev
[STRING_SIZE
];
45 char default_gateway
[STRING_SIZE
];
56 fprintf(stderr
, "Cannot run setuid\n");
62 /* Init the keyvalue structure */
65 /* Read in the current values */
66 if (!readkeyvalues(kv
, CONFIG_ROOT
"/ethernet/settings"))
68 fprintf(stderr
, "Cannot read ethernet settings\n");
72 /* Find the CONFIG_TYPE value */
73 if (!findkey(kv
, "CONFIG_TYPE", s
))
75 fprintf(stderr
, "Cannot read CONFIG_TYPE\n");
79 /* Check for CONFIG_TYPE=2 or 3 i.e. RED ethernet present. If not,
80 * exit gracefully. This is not an error... */
81 if (!((strcmp(s
, "1")==0) || (strcmp(s
, "2")==0) || (strcmp(s
, "3")==0) || (strcmp(s
, "4")==0)))
84 /* Now check the RED_TYPE - aliases only work with STATIC.
85 * At least, that's what /etc/rc.d/rc.netaddress.up thinks.. */
87 /* Find the RED_TYPE value */
88 if (!findkey(kv
, "RED_TYPE", s
))
90 fprintf(stderr
, "Cannot read RED_TYPE\n");
94 /* Make sure it's the right type */
95 if (!(strcmp(s
, "STATIC")==0))
98 /* Get the RED interface details */
99 if((!findkey(kv
, "RED_NETMASK", red_netmask
)) ||
100 (!findkey(kv
, "RED_DEV", red_dev
)) || (!findkey(kv
, "DEFAULT_GATEWAY", default_gateway
)))
102 fprintf(stderr
, "Cannot read RED settings\n");
106 if (!VALID_DEVICE(red_dev
))
108 fprintf(stderr
, "Bad red_dev: %s\n", red_dev
);
112 if (!VALID_IP(red_netmask
))
114 fprintf(stderr
, "Bad red_netmask : %s\n", red_netmask
);
118 if (!VALID_IP(default_gateway
))
120 fprintf(stderr
, "Bad default_gateway : %s\n", default_gateway
);
124 // Flush all previous aliases
127 snprintf(command
, STRING_SIZE
- 1,
128 "ip addr flush dev red%d scope %d 2>/dev/null", alias
++, SCOPE
);
129 } while (safe_system(command
) == 0);
131 /* Now set up the new aliases from the config file */
132 if (!(file
= fopen(CONFIG_ROOT
"/ethernet/aliases", "r")))
134 fprintf(stderr
, "Unable to open aliases configuration file\n");
140 while (fgets(s
, STRING_SIZE
, file
) != NULL
)
143 if (s
[strlen(s
) - 1] == '\n')
144 s
[strlen(s
) - 1] = '\0';
150 sptr
= strtok(s
, ",");
162 sptr
= strtok(NULL
, ",");
165 if (!(aliasip
&& enabled
)) {
166 fprintf(stderr
, "Incomplete data line: in %s(%d)\n",
167 CONFIG_ROOT
"/ethernet/aliases",
171 if (!strcmp(enabled
, "on") == 0) /* disabled rule? */
174 if (!VALID_IP(aliasip
))
176 fprintf(stderr
, "Bad alias : %s in %s(%d)\n",
178 CONFIG_ROOT
"/ethernet/aliases",
183 // Default to RED_DEV if intf isn't set
187 snprintf(command
, STRING_SIZE
- 1, "ip addr add %s/%s dev %s scope %d",
188 aliasip
, red_netmask
, intf
, SCOPE
);
189 safe_system(command
);