Browse Source

broken into two udebs

netcfg-dhcp 6k
netcfg-static 9k

They share a fair amount of code right now (3k) (a small shared library may be
in order, or perhaps they should be re-merged? Probably doesn't make sense to
do anything until later when ppp and pcmcia have been added because things may
change.)

r285
master
David Whedon 22 years ago
parent
commit
0f525a3b1c
  1. 29
      Makefile
  2. 3
      TODO
  3. 8
      debian/changelog
  4. 26
      debian/control
  5. 7
      debian/netcfg-dhcp.templates
  6. 55
      debian/netcfg-static.templates
  7. 6
      debian/rules
  8. 121
      debian/templates
  9. 564
      netcfg.c
  10. 170
      util.c
  11. 39
      util.h

29
Makefile

@ -1,3 +1,8 @@
ifndef PROGS
PROGS=netcfg-dhcp netcfg-static
endif
INCS=-I../cdebconf/src/
LDOPTS=-L../cdebconf/src/ -ldebconf
PREFIX=$(DESTDIR)/usr/
@ -7,18 +12,22 @@ STRIPTOOL=strip
STRIP = $(STRIPTOOL) --remove-section=.note --remove-section=.comment
OBJS=util.o
all: netcfg
$(STRIP) netcfg
size netcfg
netcfg: netcfg.c $(OBJS)
$(CC) $(CFLAGS) netcfg.c -o netcfg $(INCS) $(LDOPTS) $(OBJS)
all: $(PROGS)
install:
mkdir -p $(PREFIX)/bin/
$(INSTALL) netcfg $(PREFIX)/bin/
$(foreach PROG, $(PROGS), \
cp $(PROG) debian/$(PROG).config)
netcfg-dhcp: netcfg.c
$(CC) $(CFLAGS) -DDHCP netcfg.c -o $@ $(INCS) $(LDOPTS)
$(STRIP) $@
size $@
netcfg-static: netcfg.c
$(CC) $(CFLAGS) -DSTATIC netcfg.c -o $@ $(INCS) $(LDOPTS)
$(STRIP) $@
size $@
clean:
rm -f *.o netcfg
rm -f netcfg-*

3
TODO

@ -1,6 +1,6 @@
* put in checks so the user can't enter data that is clearly invalid (perhaps this will be done through debconf.
* fall back to safe defaults if the user doesn't enter a particular value (ex. gateway)
* do other sorts of network configurations (pcmcia)
* do other sorts of network configurations (pcmcia, ppp)
* inderstand the target_path() thing, and do it properly
* what do we do differently if it is a CD filesystem?
* decide what numbers you will actually present the user with for confirmation (you have overkill now)
@ -8,3 +8,4 @@
* currently we get an internal error if the user has lots of network interfaces, find a better way of handling that.
* etherconf has some good descriptive text, perhaps I want to use it in place of the existing text.
* when the real dhcpcd installs itself we want to make sure it doesn't destroy our nice working configuration, how to do that?
* dhcp and static config udebs share a fair amount of code, perhaps a shared library?

8
debian/changelog

@ -1,4 +1,10 @@
netcfg (0.1-1) unstable; urgency=low
netcfg (0.2) unstable; urgency=low
* dhcp and static config now in two separate udebs
-- David Whedon <dwhedon@gordian.com> Tue, 26 Dec 2000 20:53:58 -0800
netcfg (0.1) unstable; urgency=low
* Initial Release.

26
debian/control

@ -1,14 +1,28 @@
Source: netcfg
Section: debian-installer
Priority: standard
Priority: optional
Maintainer: David Whedon <dwhedon@gordian.com>
Build-Depends: debhelper (>= 2.1.18), dpkg-dev (>= 1.7.0), cdebconf-dev
Standards-Version: 3.1.1
Package: netcfg
Package: netcfg-dhcp
Architecture: any
Depends: ${shlibs:Depends}, dhcpcd-udeb
Provides: network-configuration
Depends: cdebconf-udeb, dhcpcd-udeb
Provides: configured-network
XBC-Installer-Menu-Item: 2
Description: Configure the network
This package configures the network.
Description: Configure the network via DHCP
Many networks allow automatic configuration via DHCP (Dynamic Host
Configuration Protocol). If you are on a corporate network, a cable modem,
some DSL connections, or have any other sort of dynamic IP for Ethernet, you
will likely be able to configure via DHCP. If you have information such as IP
address, netmask, gateway, then DHCP is not appropriate.
Package: netcfg-static
Architecture: any
Depends: cdebconf-udeb
Provides: configured-network
XBC-Installer-Menu-Item: 2
Description: Configure a static network
If you have information such as IP address, netmask, gateway, then you will be
able to configure a static network. If you don't know this information, you
should ask you system administrator.

7
debian/netcfg-dhcp.templates

@ -0,0 +1,7 @@
Template: netcfg/dhcp_hostname
Type: string
Description: What is your dhcp hostname?
In some situations, you may need to supply a DHCP host name. These
situations are rare. Most commonly, it applies to cable modem users.
If you are a cable modem user, you might often need to specify an
account number here. Otherwise, you can just leave this blank.

55
debian/netcfg-static.templates

@ -0,0 +1,55 @@
Template: netcfg/get_domain
Type: string
Description: Choose the domain name.
As with individual systems, every network has a name. This name is called the
domain name. Please enter your domain name or leave this field empty if you
don't have a domain.
Template: netcfg/get_ipaddress
Type: string
Description: What is the IP address of this system?
The ipaddress identifies you system to other computer on the network. If you don't
know your ipaddress you should ask your system administrator.
Template: netcfg/get_netmask
Type: string
Description: What is your netmask?
The netmask is used to determine what ipaddresses are valid on this network. This allows
your computer to determine whether or not it needs to send packets destined for a particular
ipaddress to a gateway.
Template: netcfg/get_gateway
Type: string
Description: What is your IP gateway address?
A gateway system is one that connects your network to other networks such as
your company wide-area net or the Internet. If there is no gateway in your
subnet just leave this field blank.
Template: netcfg/gateway_unreachable
Type: note
Description: The gateway you entered is unreachable.
You may have made an error entering your ipaddress, netmask and/or gateway.
Template: netcfg/get_nameservers
Type: string
Description: Choose the DNS Server Addresses
Please enter the IP addresses (not host names) of up to 3 name servers,
separated by spaces. Do not use commas. The servers will be queried in the
order in which you enter them. If you don't want to use any name servers just
leave this field blank.
Template: netcfg/confirm_static_cfg
Type: boolean
Default: true
Description: Is this configuration correct?
interface = ${interface}
hostname = ${hostname}
domain = ${domain}
ipaddress = ${ipaddress}
netmask = ${netmask}
network = ${network}
broadcast = ${broadcast}
gateway = ${gateway}
primary DNS = ${primary_DNS}
secondary DNS = ${secondary_DNS}
tertiary DNS = ${tertiary_DNS}

6
debian/rules

@ -37,7 +37,7 @@ clean:
dh_testdir
dh_testroot
rm -f build-stamp configure-stamp
rm -f debian/*.config
# Add here commands to clean up after the build process.
-$(MAKE) clean
@ -47,7 +47,7 @@ install: build
dh_testdir
dh_testroot
dh_clean -k
dh_installdirs
# dh_installdirs
$(foreach PACKAGE, $(PACKAGES), \
( $(MAKE) install PROGS=$(PACKAGE) DESTDIR=`pwd`/debian/$(PACKAGE)) ; )
@ -61,7 +61,7 @@ binary-arch: build install
# dh_testversion
dh_testdir
dh_testroot
dh_installdebconf
dh_installdebconf
# dh_installdocs
# dh_installchangelogs
# dh_link

121
debian/templates

@ -1,121 +0,0 @@
Template: netcfg/no_interfaces
Type: note
Description: no network devices found
No network interfaces were found. That means that the installation system
was unable to find a network device. If you do have a network card, then it
is possible that the module for it hasn't been selected yet. Go back to the
'Configure Device Driver Modules' menu and load the required kernel modules
first. If your only network connection is PPP, don't worry -- you will be
asked if you want PPP later in the process.
Template: netcfg/choose_interface
Type: select
Choices: ${ifchoices}
Description: Choose an interface.
The following interfaces were detected. Choose the type of your primary
network interface that you will need for installing Debian (via NFS or HTTP).
Template: netcfg/dhcp_option
Type: boolean
Default: true
Description: Automatically configure with DHCP?
Many networks allow automatic configuration by using DHCP. If you
are on a corporate network, a cable modem, some DSL connections,
or have any other sort of dynamic IP for Ethernet, you should say yes
here. If you have information such as IP address, netmask, gateway,
then you should say no.
Template: netcfg/dhcp_hostname
Type: string
Description: What is your dhcp hostname?
In some situations, you may need to supply a DHCP host name. These
situations are rare. Most commonly, it applies to cable modem users.
If you are a cable modem user, you might often need to specify an
account number here. Otherwise, you can just leave this blank.
Template: netcfg/get_hostname
Type: string
Default: debian
Description: Choose the hostname.
Every Debian system has a name - even if it is not on a network. This name is
called its "hostname". You should now specify a hostname for your new
system. If your system is going to be on a network, you should talk to the
administrator of the network before you choose a hostname. If not, you may
choose any name you like. The hostname must not contain dots or underscores,
and you must not append the domain name here. If you can't think of a
hostname for your new system, you may press <ENTER> to use the default hostname.
Template: netcfg/get_domain
Type: string
Description: Choose the domain name.
As with individual systems, every network has a name. This name is called the
domain name. Please enter your domain name or leave this field empty if you
don't have a domain.
Template: netcfg/get_ipaddress
Type: string
Description: What is the IP address of this system?
The ipaddress identifies you system to other computer on the network. If you don't
know your ipaddress you should ask your system administrator.
Template: netcfg/get_netmask
Type: string
Description: What is your netmask?
The netmask is used to determine what ipaddresses are valid on this network. This allows
your computer to determine whether or not it needs to send packets destined for a particular
ipaddress to a gateway.
Template: netcfg/get_gateway
Type: string
Description: What is your IP gateway address?
A gateway system is one that connects your network to other networks such as
your company wide-area net or the Internet. If there is no gateway in your
subnet just leave this field blank.
Template: netcfg/gateway_unreachable
Type: note
Description: The gateway you entered is unreachable.
You may have made an error entering your ipaddress, netmask and/or gateway.
Template: netcfg/get_nameservers
Type: string
Description: Choose the DNS Server Addresses
Please enter the IP addresses (not host names) of up to 3 name servers,
separated by spaces. Do not use commas. The servers will be queried in the
order in which you enter them. If you don't want to use any name servers just
leave this field blank.
Template: netcfg/confirm_static_cfg
Type: boolean
Default: true
Description: Is this configuration correct?
interface = ${interface}
hostname = ${hostname}
domain = ${domain}
ipaddress = ${ipaddress}
netmask = ${netmask}
network = ${network}
broadcast = ${broadcast}
gateway = ${gateway}
primary DNS = ${primary_DNS}
secondary DNS = ${secondary_DNS}
tertiary DNS = ${tertiary_DNS}
Template: netcfg/error_cfg
Type: note
Description: An error occured.
Something went wrong when I tried to activate your network.
Template: netcfg/get_pcmcia_tranceiver
Type: string
Description: PCMCIA Transceiver
Some PCMCIA Cards can detect the transceiver type although this might take
some time. Others cannot detect it at all. Possible values for this parameter
are "auto", "10baseT", "10base2", "aui", and "100baseT", or a blank for cards
that don't support transceiver selection. Please specify what transceiver type
you would like to use. If you aren't sure, just use the default value of
"auto".

564
netcfg.c

@ -4,7 +4,10 @@
*/
#include <ctype.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
@ -13,173 +16,135 @@
#include <sys/stat.h>
#include <debconfclient.h>
#include "util.h"
static char *interface = NULL;
static char *hostname = NULL;
static char *domain = NULL;
static char *dhcp_hostname = NULL;
static int do_dhcp=0;
static u_int32_t ipaddress = 0;
static u_int32_t network = 0;
static u_int32_t broadcast = 0;
static u_int32_t netmask = 0;
static u_int32_t gateway = 0;
static u_int32_t nameservers[4] = { 0 };
static struct debconfclient *client;
static char buf[MAXLINE];
#ifdef DEBUG
#define INTERFACES_FILE "etc/network/interfaces"
#define HOSTS_FILE "etc/hosts"
#define NETWORKS_FILE "etc/networks"
#define RESOLV_FILE "etc/resolv.conf"
#define DHCPCD_FILE "etc/dhcpc/config"
#else
#define INTERFACES_FILE "/etc/network/interfaces"
#define HOSTS_FILE "/etc/hosts"
#define NETWORKS_FILE "/etc/networks"
#define RESOLV_FILE "/etc/resolv.conf"
#define DHCPCD_FILE "/etc/dhcpc/config"
#endif
#define MAXLINE 1024
static char *interface = NULL;
static char *hostname = NULL;
static struct debconfclient *client;
static char buf[MAXLINE];
/**
* dot2num and num2dot
* Copyright: Karl Hammar, Aspö Data
*/
char *
dot2num (u_int32_t * num, char *dot)
{
char *p = dot - 1;
char *e;
int ix;
unsigned long val;
int is_interface_up (char *inter);
void getif_start (void);
void getif_end (void);
char * getif (int all);
void get_name (char *name, char *p);
char * get_ifdsc (const char *ifp);
int get_interface ();
FILE * file_open (char *path);
int activate_net ();
if (!dot)
return NULL;
*num = 0;
for (ix = 0; ix < 4; ix++)
{
*num <<= 8;
p++;
val = strtoul (p, &e, 10);
if (e == p)
val = 0;
else if (val > 255)
return NULL;
*num += val;
/*printf("%#8x, %#2x\n", *num, val); */
if (ix < 3 && *e != '.')
return NULL;
p = e;
}
void debconf_unseen (char *template);
void debconf_subst (char *template, char *key, char *string);
char * debconf_input (char *priority, char *template);
return p;
}
#ifdef DHCP
static char *dhcp_hostname = NULL;
static char num2dot_buf[16];
void write_dhcp_cfg(void);
char *
num2dot (u_int32_t num)
int
main (int argc, char *argv[])
{
int byte[4];
int ix;
char *dot = num2dot_buf;
client = debconfclient_new ();
if (num == 0)
return NULL;
client->command (client, "title", "Network Configuration", NULL);
for (ix = 3; ix >= 0; ix--)
{
byte[ix] = num & 0xff;
num >>= 8;
}
sprintf (dot, "%d.%d.%d.%d", byte[0], byte[1], byte[2], byte[3]);
client->command (client, "input", "high", "netcfg/dhcp_hostname", NULL);
client->command (client, "go", NULL);
client->command (client, "get", "netcfg/dhcp_hostname", NULL);
dhcp_hostname = client->value;
write_dhcp_cfg();
activate_dhcp_net();
return dot;
return 0;
}
void
write_dhcp_cfg(void){
char *
debconf_input (char *priority, char *template)
{
client->command (client, "input", priority, template, NULL);
client->command (client, "go", NULL);
client->command (client, "get", template, NULL);
return client->value;
FILE *fp;
if ((fp = file_open (INTERFACES_FILE)))
{
fprintf (fp, "\n# This entry was created during the Debian installation\n");
fprintf (fp, "iface %s inet dhcp\n", interface);
}
if ((fp = file_open (DHCPCD_FILE)))
{
fprintf (fp, "\n# dhcpcd configuration: created during the Debian installation\n");
fprintf (fp, "IFACE=%s", interface);
if (dhcp_hostname)
fprintf (fp, "OPTIONS='-h %s'", dhcp_hostname);
}
}
void
debconf_subst (char *template, char *key, char *string)
int
activate_dhcp_net ()
{
if (string)
client->command (client, "subst", template, key, string, NULL);
else
client->command (client, "subst", template, key, "<none>", NULL);
}
char *ptr;
int rv;
system ("/sbin/ifconfig lo 127.0.0.1");
ptr=buf;
ptr += snprintf (buf, sizeof (buf), "/sbin/dhcpcd-2.2.x");
if (dhcp_hostname)
snprintf(ptr, sizeof(buf)-(ptr-buf), " -h %s", dhcp_hostname);
void
debconf_unseen (char *template)
{
client->command (client, "fset", template, "seen", "false", NULL);
rv = system (buf);
fprintf(stderr, "rv = %d\n", rv);
if (rv != 0) {
client->command (client, "input", "critical", "netcfg/error_cfg", NULL);
client->command (client, "go", NULL);
}
return 0;
}
/*
* Get all available interfaces from the kernel and ask the user which one
* he wants to configure
*/
int
get_interface ()
{
char *ptr = buf;
char *inter;
debconf_unseen ("netcfg/choose_interface");
#endif /* DHCP */
getif_start ();
while ((inter = getif (1)) != NULL)
{
ptr +=
snprintf (ptr, sizeof (buf) - strlen (buf), "%s: %s, ", inter,
get_ifdsc (inter));
if (ptr > (buf + sizeof (buf)))
{
fprintf (stderr, "Internal error.\n");
exit (1);
}
}
getif_end ();
#ifdef STATIC
if (ptr == buf)
{
client->command (client, "input", "high", "netcfg/no_interfaces", NULL);
client->command (client, "go", NULL);
return -1;
}
static char *domain = NULL;
static u_int32_t ipaddress = 0;
static u_int32_t network = 0;
static u_int32_t broadcast = 0;
static u_int32_t netmask = 0;
static u_int32_t gateway = 0;
static u_int32_t nameservers[4] = { 0 };
debconf_subst ("netcfg/choose_interface", "ifchoices", buf);
static struct debconfclient *client;
ptr = debconf_input ("critical", "netcfg/choose_interface");
/* grab just the interface name, not the description too */
*(strchr (ptr, ':')) = '\0';
static char buf[MAXLINE];
interface = strdup (ptr);
debconf_subst ("netcfg/confirm_static_cfg", "interface", interface);
void get_static_cfg (void);
void write_static_cfg (void);
char * dot2num (u_int32_t * num, char *dot);
char * num2dot (u_int32_t num);
int
main (int argc, char *argv[])
{
client = debconfclient_new ();
client->command (client, "title", "Network Configuration", NULL);
get_static_cfg ();
write_static_cfg ();
activate_static_net();
return 0;
}
void
get_static_cfg (void)
{
@ -287,21 +252,6 @@ get_static_cfg (void)
}
FILE *
file_open (char *path)
{
FILE *fp;
if ((fp = fopen (path, "w")))
return fp;
else
{
perror ("fopen");
return NULL;
}
}
void
write_static_cfg (void)
@ -359,46 +309,72 @@ write_static_cfg (void)
}
/**
* dot2num and num2dot
* Copyright: Karl Hammar, Aspö Data
*/
char *
dot2num (u_int32_t * num, char *dot)
{
char *p = dot - 1;
char *e;
int ix;
unsigned long val;
if (!dot)
return NULL;
*num = 0;
for (ix = 0; ix < 4; ix++)
{
*num <<= 8;
p++;
val = strtoul (p, &e, 10);
if (e == p)
val = 0;
else if (val > 255)
return NULL;
*num += val;
/*printf("%#8x, %#2x\n", *num, val); */
if (ix < 3 && *e != '.')
return NULL;
p = e;
}
return p;
}
static char num2dot_buf[16];
void
write_dhcp_cfg(void){
char *
num2dot (u_int32_t num)
{
int byte[4];
int ix;
char *dot = num2dot_buf;
FILE *fp;
if ((fp = file_open (INTERFACES_FILE)))
if (num == 0)
return NULL;
for (ix = 3; ix >= 0; ix--)
{
fprintf (fp, "\n# This entry was created during the Debian installation\n");
fprintf (fp, "iface %s inet dhcp\n", interface);
byte[ix] = num & 0xff;
num >>= 8;
}
if ((fp = file_open (DHCPCD_FILE)))
{
fprintf (fp, "\n# dhcpcd configuration: created during the Debian installation\n");
fprintf (fp, "IFACE=%s", interface);
if (dhcp_hostname)
fprintf (fp, "OPTIONS='-h %s'", dhcp_hostname);
}
}
sprintf (dot, "%d.%d.%d.%d", byte[0], byte[1], byte[2], byte[3]);
return dot;
}
int
activate_net ()
activate_static_net ()
{
char *ptr;
int rv;
system ("/sbin/ifconfig lo 127.0.0.1");
if (do_dhcp) {
ptr=buf;
ptr += snprintf (buf, sizeof (buf), "/sbin/dhcpcd-2.2.x");
if (dhcp_hostname)
snprintf(ptr, sizeof(buf)-(ptr-buf), " -h %s", dhcp_hostname);
}
else {
snprintf (buf, sizeof (buf),
"/sbin/ifconfig %s %s netmask %s broadcast %s", interface,
num2dot (ipaddress), num2dot (netmask), num2dot (broadcast));
}
rv = system (buf);
fprintf(stderr, "rv = %d\n", rv);
@ -410,31 +386,253 @@ activate_net ()
}
#endif /* STATIC */
char *
debconf_input (char *priority, char *template)
{
client->command (client, "input", priority, template, NULL);
client->command (client, "go", NULL);
client->command (client, "get", template, NULL);
return client->value;
}
void
debconf_subst (char *template, char *key, char *string)
{
if (string)
client->command (client, "subst", template, key, string, NULL);
else
client->command (client, "subst", template, key, "<none>", NULL);
}
void
debconf_unseen (char *template)
{
client->command (client, "fset", template, "seen", "false", NULL);
}
int
main (int argc, char *argv[])
is_interface_up (char *inter)
{
struct ifreq ifr;
int sfd = -1, ret = -1;
char *ptr;
client = debconfclient_new ();
if ((sfd = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
goto int_up_done;
client->command (client, "title", "Network Configuration", NULL);
strncpy (ifr.ifr_name, inter, sizeof (ifr.ifr_name));
if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
goto int_up_done;
ret = (ifr.ifr_flags & IFF_UP) ? 1 : 0;
int_up_done:
if (sfd != -1)
close (sfd);
return ret;
}
static FILE *ifs = NULL;
static char ibuf[512];
void
getif_start (void)
{
if (ifs != NULL)
{
fclose (ifs);
ifs = NULL;
}
if ((ifs = fopen ("/proc/net/dev", "r")) != NULL)
{
fgets (ibuf, sizeof (ibuf), ifs); /* eat header */
fgets (ibuf, sizeof (ibuf), ifs); /* ditto */
}
return;
}
void
getif_end (void)
{
if (ifs != NULL)
{
fclose (ifs);
ifs = NULL;
}
return;
}
char *
getif (int all)
{
char rbuf[512];
if (ifs == NULL)
return NULL;
if (fgets (rbuf, sizeof (rbuf), ifs) != NULL)
{
get_name (ibuf, rbuf);
if (!strcmp (ibuf, "lo")) /* ignore the loopback */
return getif (all); /* seriously doubt there is an infinite number of lo devices */
if (all || is_interface_up (ibuf) == 1)
return ibuf;
}
return NULL;
}
void
get_name (char *name, char *p)
{
while (isspace (*p))
p++;
while (*p)
{
if (isspace (*p))
break;
if (*p == ':')
{ /* could be an alias */
char *dot = p, *dotname = name;
*name++ = *p++;
while (isdigit (*p))
*name++ = *p++;
if (*p != ':')
{ /* it wasn't, backup */
p = dot;
name = dotname;
}
if (*p == '\0')
return;
p++;
break;
}
*name++ = *p++;
}
*name++ = '\0';
return;
}
ptr = debconf_input ("critical", "netcfg/dhcp_option");
if (strstr (ptr, "true"))
/*
* Get a description for an interface (i.e. "Ethernet" for ethX).
*/
char *
get_ifdsc (const char *ifp)
{
int i;
struct if_alist_struct
{
do_dhcp=1;
dhcp_hostname = debconf_input ("high", "netcfg/dhcp_hostname");
write_dhcp_cfg();
char *interface;
char *description;
}
else
interface_alist[] =
{
get_static_cfg ();
write_static_cfg ();
}
/*
* A _("string") is an element of a char *[], and the linker will
* know where to find the elements. This works with `pointerize';
* perhaps that's different with `gettext'... though if it expands
* to a function call, this initializer should still work fine.
* Also see the 1.66 version of choose_cdrom(), which uses the
* similar technique. If it works there, it will work here.
*/
{
"eth", "Ethernet or Fast Ethernet"}
,
{
"pcmcia", "PC-Card (PCMCIA) Ethernet or Token Ring"}
,
{
"tr", "Token Ring"}
,
{
"arc", "Arcnet"}
,
{
"slip", "Serial-line IP"}
,
{
"plip", "Parallel-line IP"}
,
{
NULL, NULL}
};
for (i = 0; interface_alist[i].interface != NULL; i++)
if (!strncmp (ifp, interface_alist[i].interface,
strlen (interface_alist[i].interface)))
return interface_alist[i].description;
return NULL;
}
/*
* Get all available interfaces from the kernel and ask the user which one
* he wants to configure
*/
int
get_interface ()
{
char *ptr = buf;
char *inter;
activate_net();
debconf_unseen ("netcfg/choose_interface");
getif_start ();
while ((inter = getif (1)) != NULL)
{
ptr +=
snprintf (ptr, sizeof (buf) - strlen (buf), "%s: %s, ", inter,
get_ifdsc (inter));
if (ptr > (buf + sizeof (buf)))
{
fprintf (stderr, "Internal error.\n");
exit (1);
}
}
getif_end ();
if (ptr == buf)
{
client->command (client, "input", "high", "netcfg/no_interfaces", NULL);
client->command (client, "go", NULL);
return -1;
}
debconf_subst ("netcfg/choose_interface", "ifchoices", buf);
ptr = debconf_input ("critical", "netcfg/choose_interface");
/* grab just the interface name, not the description too */
*(strchr (ptr, ':')) = '\0';
interface = strdup (ptr);
debconf_subst ("netcfg/confirm_static_cfg", "interface", interface);
return 0;
}
FILE *
file_open (char *path)
{
FILE *fp;
if ((fp = fopen (path, "w")))
return fp;
else
{
perror ("fopen");
return NULL;
}
}

170
util.c

@ -1,170 +0,0 @@
/*
* file: util.c
* These functions were taken mostly untouched from dbootstrap
*
* */
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include "util.h"
int
is_interface_up (char *inter)
{
struct ifreq ifr;
int sfd = -1, ret = -1;
if ((sfd = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
goto int_up_done;
strncpy (ifr.ifr_name, inter, sizeof (ifr.ifr_name));
if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0)
goto int_up_done;
ret = (ifr.ifr_flags & IFF_UP) ? 1 : 0;
int_up_done:
if (sfd != -1)
close (sfd);
return ret;
}
static FILE *ifs = NULL;
static char ibuf[512];
void
getif_start (void)
{
if (ifs != NULL)
{
fclose (ifs);
ifs = NULL;
}
if ((ifs = fopen ("/proc/net/dev", "r")) != NULL)
{
fgets (ibuf, sizeof (ibuf), ifs); /* eat header */
fgets (ibuf, sizeof (ibuf), ifs); /* ditto */
}
return;
}
void
getif_end (void)
{
if (ifs != NULL)
{
fclose (ifs);
ifs = NULL;
}
return;
}
char *
getif (int all)
{
char rbuf[512];
if (ifs == NULL)
return NULL;
if (fgets (rbuf, sizeof (rbuf), ifs) != NULL)
{
get_name (ibuf, rbuf);
if (!strcmp (ibuf, "lo")) /* ignore the loopback */
return getif (all); /* seriously doubt there is an infinite number of lo devices */
if (all || is_interface_up (ibuf) == 1)
return ibuf;
}
return NULL;
}
void
get_name (char *name, char *p)
{
while (isspace (*p))
p++;
while (*p)
{
if (isspace (*p))
break;
if (*p == ':')
{ /* could be an alias */
char *dot = p, *dotname = name;
*name++ = *p++;
while (isdigit (*p))
*name++ = *p++;
if (*p != ':')
{ /* it wasn't, backup */
p = dot;
name = dotname;
}
if (*p == '\0')
return;
p++;
break;
}
*name++ = *p++;
}
*name++ = '\0';
return;
}
/*
* Get a description for an interface (i.e. "Ethernet" for ethX).
*/
char *
get_ifdsc (const char *ifp)
{
int i;
struct if_alist_struct
{
char *interface;
char *description;
}
interface_alist[] =
{
/*
* A _("string") is an element of a char *[], and the linker will
* know where to find the elements. This works with `pointerize';
* perhaps that's different with `gettext'... though if it expands
* to a function call, this initializer should still work fine.
* Also see the 1.66 version of choose_cdrom(), which uses the
* similar technique. If it works there, it will work here.
*/
{
"eth", "Ethernet or Fast Ethernet"}
,
{
"pcmcia", "PC-Card (PCMCIA) Ethernet or Token Ring"}
,
{
"tr", "Token Ring"}
,
{
"arc", "Arcnet"}
,
{
"slip", "Serial-line IP"}
,
{
"plip", "Parallel-line IP"}
,
{
NULL, NULL}
};
for (i = 0; interface_alist[i].interface != NULL; i++)
if (!strncmp (ifp, interface_alist[i].interface,
strlen (interface_alist[i].interface)))
return interface_alist[i].description;
return NULL;
}

39
util.h

@ -1,39 +0,0 @@
#ifndef __UTIL_H
#define __UTIL_H
#define MAXLINE 1024
/*
* Get a description for an interface (i.e. "Ethernet" for ethX).
*/
extern char *get_ifdsc (const char *ifp);
/* Checks to see if any network interfaces are active (excepting the
* loopback interface "lo" - Returns the number of active interfaces. If
* all is non-zero, all interfaces are counted, even if not up */
int is_network_up (int all);
/* Checks to see if the named interface, "inter", is up - Returns 1 if it
* is, 0 if it is down. -1 is returned if the interface does not exist. */
int is_interface_up (char *inter);
/* Takes the buffer "p" and reads the interface name from it. "p" must be
* a full line read in from /proc/net/dev. The interface name is places
* into the "name" buffer (which must already be allocated) */
void get_name (char *name, char *p);
/* Get listing of interfaces on the system. getif_start() must be called
* first in order to initialize the setup. After which, each successive
* call get getif() will return a pointer to the next interface, ending
* with NULL, when no more interfaces are available. If you call getif and
* all is non-zero, it will return all interfaces, even ones that are not
* active. Finally when done, call getif_end().
*/
void getif_start (void);
char *getif (int all);
void getif_end (void);
#endif /* __UTIL_H */
Loading…
Cancel
Save