|
From: <ow...@us...> - 2008-01-28 20:52:13
|
Revision: 1042
http://ipcop.svn.sourceforge.net/ipcop/?rev=1042&view=rev
Author: owes
Date: 2008-01-28 12:52:17 -0800 (Mon, 28 Jan 2008)
Log Message:
-----------
Yet Another Micro Installer Update.
After this YAMIU it should be possible to detect and assign network cards.
Not working (yet):
manual addition of card, for example for ISA
network changes in setup
For the next step I intend to drop CONFIG_TYPE from ethernet/settings and maybe rename GREEN_ BLUE_ etc. to GREEN_1_ BLUE_1_ (makes configuring multiple cards with same colour a helluvalot easier).
Modified Paths:
--------------
ipcop/trunk/src/installer/networking.c
Modified: ipcop/trunk/src/installer/networking.c
===================================================================
--- ipcop/trunk/src/installer/networking.c 2008-01-28 20:45:01 UTC (rev 1041)
+++ ipcop/trunk/src/installer/networking.c 2008-01-28 20:52:17 UTC (rev 1042)
@@ -11,6 +11,7 @@
*/
+#include <dirent.h>
#include <libintl.h>
#include <malloc.h>
#include <newt.h>
@@ -24,45 +25,164 @@
/* Length of list with types for red */
#define CFG_RED_COUNT 6
+/* How many colours do we have */
+#define CFG_COLOURS_COUNT 5
+struct network_s
+{
+ char *module; /* kernel module */
+ char *options; /* modprobe parameters */
+ char *device; /* eth0, eth1 etc. */
+ char *description;
+ char colour[10]; /* GREEN, RED, BLUE, ORANGE, ---- */
+ char *address; /* MAC address */
+};
+
+
static NODEKV *eth_kv;
+static char kv_red_type[STRING_SIZE] = ""; /* extra variable for red type */
+static int hardware_scanned = 0;
+static struct network_s *networks; /* make our own table for easier handling */
+
static int changed_config; /* something has changed */
static int changed_type; /* red type */
static int changed_hostname; /* hostname (DHCP Red) */
static int changed_green; /* IP and netmask green */
static int changed_red; /* IP and netmask red */
+static int changed_blue; /* IP and netmask blue */
+static int changed_orange; /* IP and netmask orange */
static int changed_dnsgateway; /* DNS1, DNS2 and gateway */
static char default_ip[STRING_SIZE] = "192.168.1.1";
static char default_netmask[STRING_SIZE] = "255.255.255.0";
+static char *all_colours[CFG_COLOURS_COUNT] = { "GREEN", "RED", "BLUE", "ORANGE", "----" }; /* use ---- for non-assigned card */
-
static unsigned int numnics;
static struct hardware_s *nics;
-static void build_card_list(void)
+/*
+ Filter function for scanning directory /sys/class/net
+*/
+static int filter_net(const struct dirent *d)
{
+ int i;
+
+ if ( !strcmp(d->d_name, "lo") || !strcmp(d->d_name, ".") || !strcmp(d->d_name, "..") )
+ {
+ /* these are not really interesting */
+ return 0;
+ }
+
+ for (i = 0; i < numnetwork; i++)
+ {
+ if ( networks[i].device[0] && !strcmp(d->d_name, networks[i].device) )
+ {
+ /* we've already got this one listed */
+ return 0;
+ }
+ }
+
+ /* not in our list (yet) */
+ return 1;
}
+/*
+ modprobe some module, then look for new net device
+*/
+static void probe_card(char *module, char *options)
+{
+ char command[STRING_SIZE];
+ struct dirent **scndir;
+ int newcards;
+ int i, j;
+
+ /* owes: ToDo test if module already loaded ? */
+
+ snprintf(command, STRING_SIZE, "/sbin/modprobe %s %s", module, options);
+ if ( mysystem(command) )
+ return NULL;
+
+ /* new card found ? */
+ newcards = scandir ("/sys/class/net", &scndir, filter_net, alphasort);
+ if ( newcards )
+ {
+ fprintf(flog, "probed %s, found %d\n", module, newcards);
+
+ for (i = 0, j = 0; (i < numnetwork) && (j < newcards); i++)
+ {
+ if ( !networks[i].device[0] && !strcmp(module, networks[i].module) )
+ {
+ FILE *f;
+ char buffer[STRING_SIZE];
+
+ free(networks[i].device);
+ networks[i].device = strdup(scndir[j]->d_name);
+ /* get and store MAC address */
+ snprintf(buffer, STRING_SIZE, "/sys/class/net/%s/address", networks[i].device);
+ f = fopen(buffer, "r");
+ if ( f != NULL )
+ {
+ if ( fgets(buffer, STRING_SIZE, f) )
+ {
+ stripnl(buffer);
+ networks[i].address = strdup(buffer);
+ }
+ fclose(f);
+ }
+ j++;
+ fprintf(flog, " hwlist %s device %s MAC %s\n", module, networks[i].device, networks[i].address);
+ }
+ }
+
+ if ( j < newcards )
+ {
+ fprintf(flog, " some stray modprobe?\n");
+ }
+ }
+}
+
+
+/*
+ Use discover to find hardware, build list with detected network cards
+*/
static void scan_cards(void)
{
int i;
+ if ( hardware_scanned )
+ {
+ return;
+ }
+ hardware_scanned = 1;
+
/* scan for NICs, disable SCSI */
scan_hardware(0, 0, 0, 1);
+ numnetwork = 0;
+ /* setup our own table */
for (i = 0; i < numhardwares; i++)
{
if ( hardwares[i].type == network )
{
- char command[STRING_SIZE];
- snprintf(command, STRING_SIZE, "modprobe %s", hardwares[i].module);
- mysystem(command);
+ networks = realloc(networks, sizeof(struct network_s) * (numnetwork + 1));
+ networks[numnetwork].module = hardwares[i].module;
+ networks[numnetwork].options = strdup("");
+ networks[numnetwork].device = strdup("");
+ networks[numnetwork].description = hardwares[i].description;
+ strcpy(networks[numnetwork].colour, "----");
+ networks[numnetwork].address = strdup("");
+
+ numnetwork++;
}
}
+
+ for (i = 0; i < numnetwork; i++)
+ {
+ probe_card(networks[i].module, "");
+ }
}
@@ -91,7 +211,7 @@
char message[STRING_SIZE_LARGE];
int numLines;
- snprintf(message, STRING_SIZE, gettext("TR_RED_CONFIGURATION_TYPE"));
+ snprintf(message, STRING_SIZE, gettext("TR_RED_CONFIGURATION_TYPE_LONG"));
text = newtTextboxReflowed(1, 1, message, 68, 0, 0, 0);
numLines = newtTextboxGetNumLines(text);
@@ -137,7 +257,9 @@
{
/* config type has changed, update cfg file and set flag */
update_kv(ð_kv, "RED_TYPE", cfg_text[i]);
+ strcpy(kv_red_type, cfg_text[i]);
+ /* keep CONFIG_TYPE for now */
if ( !strcmp(cfg_text[i], "ANALOG") || !strcmp(cfg_text[i], "ISDN") )
{
update_kv(ð_kv, "CONFIG_TYPE", "0");
@@ -159,88 +281,159 @@
/*
- W.I.P.
+ Update some settings
*/
-static void cardconfig(void)
+static void update_settings(char *colour, int n)
{
- int i;
- newtComponent networkform;
- newtComponent infolabel;
- newtComponent ok, cancel;
- struct newtExitStruct exitstruct;
- char keyvalue[STRING_SIZE];
- char info[STRING_SIZE_LARGE];
+ char key[STRING_SIZE];
+ char key_value[STRING_SIZE];
- newtComponent devicelabel;
- newtComponent deviceentry;
- const char *driverresult;
- newtComponent driverlabel;
- newtComponent driverentry;
- const char *deviceresult;
+ fprintf(flog, "colour %s, index %d\n", colour, n);
- scan_cards();
-
- if ( flag_installer )
+ if ( n == -1 )
{
- /* as long as scanning is not working properly, do a quick setup for green */
+ /* was occupied, now empty */
+ snprintf(key, STRING_SIZE, "%s_DEV", colour);
+ update_kv(ð_kv, key, "");
+ snprintf(key, STRING_SIZE, "%s_DRIVER", colour);
+ update_kv(ð_kv, key, "");
+ }
+ else
+ {
+ snprintf(key, STRING_SIZE, "%s_DEV", colour);
+ update_kv(ð_kv, key, networks[n].device);
+ snprintf(key, STRING_SIZE, "%s_DRIVER", colour);
+ update_kv(ð_kv, key, networks[n].module);
+ }
+}
- newtCenteredWindow(60, 14, gettext("TR_CARD_ASSIGNMENT"));
- networkform = newtForm(NULL, NULL, 0);
- driverlabel = newtTextbox(2, 4, 18, 1, 0);
- newtTextboxSetText(driverlabel, "Green Driver:");
- strcpy(keyvalue, "e100");
- find_kv_default(eth_kv, "GREEN_DRIVER", keyvalue);
- driverentry = newtEntry(20, 4, keyvalue, 20, &driverresult, 0);
- newtFormAddComponent(networkform, driverlabel);
- newtFormAddComponent(networkform, driverentry);
+/*
+ Assign 1 card to some colour
+*/
+static void cardconfig(int n)
+{
+ char info[STRING_SIZE_LARGE];
+ int i, j;
+ int rc, choice;
+ char *colourchoices[10];
+ char command[STRING_SIZE];
- devicelabel = newtTextbox(2, 5, 18, 1, 0);
- newtTextboxSetText(devicelabel, "Green Device:");
- strcpy(keyvalue, "eth0");
- find_kv_default(eth_kv, "GREEN_DEV", keyvalue);
- deviceentry = newtEntry(20, 5, keyvalue, 20, &deviceresult, 0);
- newtFormAddComponent(networkform, devicelabel);
- newtFormAddComponent(networkform, deviceentry);
+ /* owes: ToDo could do with some fancy text here */
+ snprintf(info, STRING_SIZE_LARGE, "%s\nMAC Address: %s Device: %s\nCurrently assigned to: %s",
+ networks[n].description, networks[n].address, networks[n].device, networks[n].colour);
+
+ for (choice = 0, i = 0; i < CFG_COLOURS_COUNT-1; i++)
+ {
+ int used = 0;
- ok = newtButton(8,10, gettext("TR_OK"));
- cancel = newtButton(26,10, gettext("TR_CANCEL"));
- newtFormAddComponents(networkform, ok, cancel, NULL);
+ /* skip RED if type is ANALOG or ISDN */
+ if ( (i == 1) && (!strcmp(kv_red_type, "ANALOG") || !strcmp(kv_red_type, "ISDN")) )
+ {
+ continue;
+ }
- newtRefresh();
- newtDrawForm(networkform);
+ /* test for already used colours here */
+ for (j = 0; j < numnetwork && !used; j++)
+ {
- newtFormRun(networkform, &exitstruct);
+ if ( !strcmp(all_colours[i], networks[j].colour) )
+ {
+ used = 1;
+ }
+ }
- update_kv(ð_kv, "GREEN_DRIVER", (char *)driverresult);
- update_kv(ð_kv, "GREEN_DEV", (char *)deviceresult);
+ if ( !used )
+ {
+ colourchoices[choice++] = all_colours[i];
+ }
+ }
+ colourchoices[choice++] = gettext("TR_NOT_USED");
+ colourchoices[choice++] = NULL;
- newtFormDestroy(networkform);
- newtPopWindow();
+ /* make the card blink during selection if supported */
+ snprintf(command, STRING_SIZE, "/sbin/ifconfig %s up", networks[n].device);
+ mysystem(command);
+ snprintf(command, STRING_SIZE, "/usr/sbin/ethtool -p %s &", networks[n].device);
+ mysystem(command);
+
+ choice = 0;
+ rc = newtWinMenu(gettext("TR_CARD_ASSIGNMENT"),
+ info, 65, 5, 5, 11,
+ colourchoices, &choice, gettext("TR_OK"), gettext("TR_QUIT"), NULL);
+
+ if ( (rc == 0) || (rc == 1) )
+ {
+ if ( !strcmp(colourchoices[choice], gettext("TR_NOT_USED")) )
+ {
+ if ( strcmp(networks[n].colour, all_colours[CFG_COLOURS_COUNT-1]) )
+ {
+ update_settings(networks[n].colour, -1);
+ }
+ strcpy(networks[n].colour, all_colours[CFG_COLOURS_COUNT-1]);
+ }
+ else
+ {
+ strcpy(networks[n].colour, colourchoices[choice]);
+ update_settings(networks[n].colour, n);
+ }
}
- else
- {
- statuswindow(60, 4, get_title(), gettext("TR_PROBING_HARDWARE"));
- newtPopWindow();
- newtCenteredWindow(60, 14, gettext("TR_CARD_ASSIGNMENT"));
- networkform = newtForm(NULL, NULL, 0);
+ strcpy(command, "/bin/killall ethtool");
+ mysystem(command);
+}
- infolabel = newtTextbox(1, 1, 56, 6, 0);
- newtTextboxSetText(infolabel, info);
- newtFormAddComponent(networkform, infolabel);
- ok = newtButton(8,10, gettext("TR_OK"));
- cancel = newtButton(26,10, gettext("TR_CANCEL"));
- newtFormAddComponents(networkform, ok, cancel, NULL);
+/*
+ Detect cards, list cards and assign colours to them.
+*/
+static void cardlist(void)
+{
+ int i, count;
+ int rc, choice;
+ char *cardchoices[10];
- newtRefresh();
- newtDrawForm(networkform);
+ scan_cards();
- newtFormRun(networkform, &exitstruct);
+ for (;;)
+ {
+ choice = -1;
+ for (count = 0, i = 0; i < numnetwork; i++)
+ {
+ char line[STRING_SIZE];
- newtFormDestroy(networkform);
- newtPopWindow();
+ /* set first unused card as default choice */
+ if ( (choice == -1) && !strcmp(networks[i].colour, "----") )
+ {
+ choice = count;
+ }
+ snprintf(line, STRING_SIZE, "%-60.60s (%s)", networks[i].description, networks[i].colour);
+ cardchoices[count++] = strdup(line);
+ }
+ cardchoices[count] = NULL;
+
+ rc = newtWinMenu(gettext("TR_CARD_ASSIGNMENT"),
+ gettext("TR_SOME_FANCY_TEXT_LONG"), 65, 5, 5, 11,
+ cardchoices, &choice, gettext("TR_OK"), gettext("TR_MANUAL"), gettext("TR_QUIT"), NULL);
+
+ for (i = 0; i < numnetwork; i++)
+ {
+ free(cardchoices[i]);
+ }
+
+ switch ( rc )
+ {
+ case 0:
+ case 1:
+ cardconfig(choice);
+ break;
+ case 2:
+ /* manually add some card/module */
+ break;
+ case 3:
+ /* owes: ToDo test if we have green and possibly red here */
+ return;
+ }
}
}
@@ -460,6 +653,62 @@
/*
+ Small window to select which colour IP to change
+*/
+static void selectchangeaddress(void)
+{
+ char *menuchoices[10];
+ char key[STRING_SIZE];
+ char keyvalue[STRING_SIZE];
+ int rc;
+ int i;
+ int choice;
+
+ choice = 0;
+ for (i = 0; i < CFG_COLOURS_COUNT; i++)
+ {
+ snprintf(key, STRING_SIZE, "%s_DEV", all_colours[i]);
+ strcpy(keyvalue, "");
+ find_kv_default(eth_kv, key, keyvalue);
+ if ( keyvalue[0] )
+ {
+ menuchoices[choice++] = all_colours[i];
+ }
+ }
+
+ menuchoices[choice] = NULL;
+
+
+ for (;;)
+ {
+ rc = newtWinMenu(gettext("TR_SELECT_THE_INTERFACE_YOU_WISH_TO_RECONFIGURE"),
+ gettext("TR_SELECT_THE_ITEM"), 65, 5, 5, 11,
+ menuchoices, &choice, gettext("TR_OK"), gettext("TR_QUIT"), NULL);
+
+ if (rc == 2)
+ break;
+
+ if ( !strcmp(menuchoices[choice], "GREEN") )
+ {
+ changeaddress("GREEN", &changed_green);
+ }
+ if ( !strcmp(menuchoices[choice], "RED") )
+ {
+ changeaddress("RED", &changed_red);
+ }
+ if ( !strcmp(menuchoices[choice], "BLUE") )
+ {
+ changeaddress("BLUE", &changed_blue);
+ }
+ if ( !strcmp(menuchoices[choice], "ORANGE") )
+ {
+ changeaddress("ORANGE", &changed_orange);
+ }
+ }
+}
+
+
+/*
Change DNS Server(s) and default gateway if type is STATIC
*/
static void changednsgateway(void)
@@ -605,8 +854,12 @@
return 0;
}
+ strcpy(kv_red_type, "");
+ find_kv_default(eth_kv, "RED_TYPE", kv_red_type);
+
if ( flag_installer )
{
+ /* When installing we run (some) configwindows in sequence */
NODEKV *kv_dhcp_params = NULL;
read_kv_from_file(&kv_dhcp_params, "/tmp/dhcp-eth0.params");
@@ -616,29 +869,44 @@
redconfigtype();
/* RED_TYPE is now set, even if cancel was pressed */
- cardconfig();
+ cardlist();
changeaddress("GREEN", &changed_green);
- find_kv_default(eth_kv, "RED_TYPE", keyvalue);
- if ( !strcmp(keyvalue, "PPPOE") )
+ if ( !strcmp(kv_red_type, "PPPOE") )
{
+ /* default to 1.1.1.1 */
+ update_kv(ð_kv, "RED_ADDRESS", "1.1.1.1");
+ /* do we want to be able to change this ? */
changeaddress("RED", &changed_red);
}
- else if ( !strcmp(keyvalue, "PPTP") )
+ else if ( !strcmp(kv_red_type, "PPTP") )
{
changeaddress("RED", &changed_red);
}
- else if ( !strcmp(keyvalue, "STATIC") )
+ else if ( !strcmp(kv_red_type, "STATIC") )
{
changeaddress("RED", &changed_red);
changednsgateway();
}
- else if ( !strcmp(find_kv(eth_kv, "RED_TYPE"), "DHCP") )
+ else if ( !strcmp(kv_red_type, "DHCP") )
{
changehostname();
changednsgateway();
}
+
+ strcpy(keyvalue, "");
+ find_kv_default(eth_kv, "BLUE_DEV", keyvalue);
+ if ( keyvalue[0] )
+ {
+ changeaddress("BLUE", &changed_blue);
+ }
+ strcpy(keyvalue, "");
+ find_kv_default(eth_kv, "ORANGE_DEV", keyvalue);
+ if ( keyvalue[0] )
+ {
+ changeaddress("ORANGE", &changed_orange);
+ }
}
else
{
@@ -660,13 +928,10 @@
redconfigtype();
break;
case 1:
- cardconfig();
+ cardlist();
break;
case 2:
- /* need another menu here */
- changeaddress("GREEN", &changed_green);
- changeaddress("RED", &changed_red);
- changehostname();
+ selectchangeaddress();
break;
case 3:
changednsgateway();
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|