diff options
author | Jordan K. Hubbard <jkh@FreeBSD.org> | 1999-09-02 00:51:16 +0000 |
---|---|---|
committer | Jordan K. Hubbard <jkh@FreeBSD.org> | 1999-09-02 00:51:16 +0000 |
commit | 3910e95363804f8a406c9dab85d29e6e9c7b3645 (patch) | |
tree | c2389a870ee9129f025e920d518eed317da234b2 /release/sysinstall/tcpip.c | |
parent | bca1bc6498161d90d4d33eb8f9ad8d89a1158be2 (diff) |
Oh crud, did I ever screw the pooch! Rather than sync this with -stable,
I backed-out the changes in -current and didn't touch stable at all (I
thought I had my patch order reversed, not what actually happened).
AIEEE! I can't even blame the crack for this one since I broke my
crack pipe a few weeks ago. I think sleep deprivation gets the blame
for this one.
Medal for noticing this one goes to: Jim Bloom <bloom@acm.org>
Notes
Notes:
svn path=/head/; revision=50780
Diffstat (limited to 'release/sysinstall/tcpip.c')
-rw-r--r-- | release/sysinstall/tcpip.c | 138 |
1 files changed, 109 insertions, 29 deletions
diff --git a/release/sysinstall/tcpip.c b/release/sysinstall/tcpip.c index 0393cfe373e2..105ed2c15875 100644 --- a/release/sysinstall/tcpip.c +++ b/release/sysinstall/tcpip.c @@ -39,6 +39,7 @@ #include "sysinstall.h" #include <sys/param.h> +#include <netdb.h> /* The help file for the TCP/IP setup screen */ #define TCP_HELPFILE "tcp" @@ -145,6 +146,53 @@ verifySettings(void) return 0; } +static void +dhcpGetInfo(Device *devp) +{ + /* If it fails, do it the old-fashioned way */ + if (dhcpParseLeases("/var/db/dhclient.leases", hostname, domainname, + nameserver, ipaddr, gateway, netmask) == -1) { + FILE *ifp; + char *cp, cmd[256], data[2048]; + int i, j; + + /* Bah, now we have to kludge getting the information from ifconfig */ + snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name); + ifp = popen(cmd, "r"); + if (ifp) { + j = fread(data, 1, sizeof(data), ifp); + fclose(ifp); + if (j < 0) /* paranoia */ + j = 0; + data[j] = '\0'; + if (isDebug()) + msgDebug("DHCP configured interface returns %s\n", data); + /* XXX This is gross as it assumes a certain ordering to + ifconfig's output! XXX */ + if ((cp = strstr(data, "inet")) != NULL) { + i = 0; + cp += 5; /* move over keyword */ + while (*cp != ' ') + ipaddr[i++] = *(cp++); + ipaddr[i] = '\0'; + if (!strncmp(++cp, "netmask", 7)) { + i = 0; + cp += 8; + while (*cp != ' ') + netmask[i++] = *(cp++); + netmask[i] = '\0'; + } + } + } + } + + /* If we didn't get a name server value, hunt for it in resolv.conf */ + if (!nameserver[0] && file_readable("/etc/resolv.conf")) + configEnvironmentResolv("/etc/resolv.conf"); + if (hostname[0]) + variable_set2(VAR_HOSTNAME, hostname, 0); +} + /* This is it - how to get TCP setup values */ int tcpOpenDialog(Device *devp) @@ -153,6 +201,7 @@ tcpOpenDialog(Device *devp) ComposeObj *obj = NULL; int n = 0, filled = 0, cancel = FALSE; int max, ret = DITEM_SUCCESS; + int use_dhcp = FALSE; char *tmp; char title[80]; @@ -163,22 +212,46 @@ tcpOpenDialog(Device *devp) SAFE_STRCPY(ipaddr, di->ipaddr); SAFE_STRCPY(netmask, di->netmask); SAFE_STRCPY(extras, di->extras); + use_dhcp = di->use_dhcp; } else { /* See if there are any defaults */ char *cp; + /* First try a DHCP scan if such behavior is desired */ + if (!variable_cmp(VAR_TRY_DHCP, "YES") || !msgYesNo("Do you want to try DHCP configuration of the interface?")) { + int k; + + Mkdir("/var/db"); + Mkdir("/var/run"); + Mkdir("/tmp"); + msgNotify("Scanning for DHCP servers..."); + for (k = 1; k < 4; k++) { + if (0 == vsystem("dhclient -1 %s", devp->name)) { + dhcpGetInfo(devp); + use_dhcp = TRUE; + break; + } + msgNotify("Scanning for DHCP servers... Retry: %d", k); + } + } + + /* Get old IP address from variable space, if available */ if (!ipaddr[0]) { if ((cp = variable_get(VAR_IPADDR)) != NULL) SAFE_STRCPY(ipaddr, cp); else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_IPADDR))) != NULL) SAFE_STRCPY(ipaddr, cp); } + + /* Get old netmask from variable space, if available */ if (!netmask[0]) { if ((cp = variable_get(VAR_NETMASK)) != NULL) SAFE_STRCPY(netmask, cp); else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_NETMASK))) != NULL) SAFE_STRCPY(netmask, cp); } + + /* Get old extras string from variable space, if available */ if (!extras[0]) { if ((cp = variable_get(VAR_EXTRAS)) != NULL) SAFE_STRCPY(extras, cp); @@ -188,26 +261,26 @@ tcpOpenDialog(Device *devp) } /* Look up values already recorded with the system, or blank the string variables ready to accept some new data */ - tmp = variable_get(VAR_HOSTNAME); - if (tmp) - SAFE_STRCPY(hostname, tmp); - else - bzero(hostname, sizeof(hostname)); - tmp = variable_get(VAR_DOMAINNAME); - if (tmp) - SAFE_STRCPY(domainname, tmp); - else - bzero(domainname, sizeof(domainname)); - tmp = variable_get(VAR_GATEWAY); - if (tmp) - SAFE_STRCPY(gateway, tmp); - else - bzero(gateway, sizeof(gateway)); - tmp = variable_get(VAR_NAMESERVER); - if (tmp) - SAFE_STRCPY(nameserver, tmp); - else - bzero(nameserver, sizeof(nameserver)); + if (!hostname[0]) { + tmp = variable_get(VAR_HOSTNAME); + if (tmp) + SAFE_STRCPY(hostname, tmp); + } + if (!domainname[0]) { + tmp = variable_get(VAR_DOMAINNAME); + if (tmp) + SAFE_STRCPY(domainname, tmp); + } + if (!gateway[0]) { + tmp = variable_get(VAR_GATEWAY); + if (tmp) + SAFE_STRCPY(gateway, tmp); + } + if (!nameserver[0]) { + tmp = variable_get(VAR_NAMESERVER); + if (tmp) + SAFE_STRCPY(nameserver, tmp); + } save = savescr(); /* If non-interactive, jump straight over the dialog crap and into config section */ @@ -252,7 +325,7 @@ reenter: * the most appropriate one (entire class C, or subnetted * class A/B network). */ - if (netmask[0] == '\0') { + if (!netmask[0]) { strcpy(netmask, "255.255.255.0"); RefreshStringObj(layout[LAYOUT_NETMASK].obj); ++filled; @@ -288,14 +361,18 @@ netconfig: char temp[512], ifn[255]; char *ifaces; - variable_set2(VAR_HOSTNAME, hostname, 1); - sethostname(hostname, strlen(hostname)); + if (hostname[0]) { + variable_set2(VAR_HOSTNAME, hostname, use_dhcp ? 0 : 1); + sethostname(hostname, strlen(hostname)); + } if (domainname[0]) variable_set2(VAR_DOMAINNAME, domainname, 0); if (gateway[0]) - variable_set2(VAR_GATEWAY, gateway, 1); + variable_set2(VAR_GATEWAY, gateway, use_dhcp ? 0 : 1); if (nameserver[0]) variable_set2(VAR_NAMESERVER, nameserver, 0); + if (ipaddr[0]) + variable_set2(VAR_IPADDR, ipaddr, 0); if (!devp->private) devp->private = (DevInfo *)safe_malloc(sizeof(DevInfo)); @@ -303,9 +380,13 @@ netconfig: SAFE_STRCPY(di->ipaddr, ipaddr); SAFE_STRCPY(di->netmask, netmask); SAFE_STRCPY(di->extras, extras); + di->use_dhcp = use_dhcp; - sprintf(temp, "inet %s %s netmask %s", ipaddr, extras, netmask); sprintf(ifn, "%s%s", VAR_IFCONFIG, devp->name); + if (use_dhcp) + sprintf(temp, "DHCP"); + else + sprintf(temp, "inet %s %s netmask %s", ipaddr, extras, netmask); variable_set2(ifn, temp, 1); ifaces = variable_get(VAR_INTERFACES); if (!ifaces) @@ -315,9 +396,8 @@ netconfig: sprintf(ifn, "%s %s", devp->name, ifaces); variable_set2(VAR_INTERFACES, ifn, 1); } - if (ipaddr[0]) - variable_set2(VAR_IPADDR, ipaddr, 0); - configResolv(NULL); /* XXX this will do it on the MFS copy XXX */ + if (!use_dhcp) + configResolv(NULL); /* XXX this will do it on the MFS copy XXX */ ret = DITEM_SUCCESS; } else @@ -396,7 +476,7 @@ tcpMenuSelect(dialogMenuItem *self) Device *tmp; tmp = tcpDeviceSelect(); - if (tmp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name)) + if (tmp && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name)) if (!tmp->init(tmp)) msgConfirm("Initialization of %s device failed.", tmp->name); return DITEM_SUCCESS | DITEM_RESTORE; |