Browse Source

broke dhcp and static cfg's into different modules so we can share

common code between them.
In theory it would save about 3k, I think, though my
initialy efforts didn't save any space, so we still aren't sharing.

r335
master
David Whedon 23 years ago
parent
commit
8063172870
  1. 34
      Makefile
  2. 135
      netcfg-dhcp.c
  3. 177
      netcfg-static.c
  4. 323
      netcfg.c
  5. 42
      netcfg.h

34
Makefile

@ -2,8 +2,19 @@ ifndef PROGS
PROGS=netcfg-dhcp netcfg-static
endif
MAJOR=0
MINOR=1
MICRO=0
LIB=libnetcfg.so
LIBNAME=libnetcfg.so.$(MAJOR).$(MINOR).$(MICRO)
SONAME=libnetcfg.so.$(MAJOR).$(MINOR)
LIBS=$(LIB) $(SONAME) $(LIBNAME)
INCS=-I../cdebconf/src/
LDOPTS=-L../cdebconf/src -ldebconf
LDOPTS=-L../cdebconf/src -ldebconf -Wl,-rpath,../cdebconf/src
#-L. -lnetcfg
PREFIX=$(DESTDIR)/usr/
CFLAGS=-Wall -Os -fomit-frame-pointer
INSTALL=install
@ -12,20 +23,29 @@ STRIP = $(STRIPTOOL) --remove-section=.note --remove-section=.comment
all: $(PROGS)
#$(LIBS)
install:
$(foreach PROG, $(PROGS), \
cp $(PROG) debian/$(PROG).postinst)
netcfg-dhcp: netcfg.c utils.o
$(CC) $(CFLAGS) -DDHCP netcfg.c utils.o -o $@ $(INCS) $(LDOPTS)
netcfg-dhcp netcfg-static: netcfg-dhcp.c utils.o netcfg.o
$(CC) $(CFLAGS) $@.c -o $@ $(INCS) $(LDOPTS) utils.o netcfg.o
$(STRIP) $@
size $@
netcfg-static: netcfg.c utils.o
$(CC) $(CFLAGS) -DSTATIC netcfg.c utils.o -o $@ $(INCS) $(LDOPTS)
$(STRIP) $@
netcfg.o:
$(CC) -c $(CFLAGS) netcfg.c -o $@ $(INCS)
$(LIBNAME): netcfg.c
@echo Creating $(LIBNAME)
$(CC) -shared -Wl,-soname,$(SONAME) -o $@ $^ $(INCS)
size $@
$(SONAME) $(LIB): $(LIBNAME)
@ln -sf $^ $@
clean:
rm -f netcfg-* *.o
rm -f netcfg-dhcp netcfg-static *.o

135
netcfg-dhcp.c

@ -0,0 +1,135 @@
/*
netcfg-dhcp.c - Configure a network via dhcp for the debian-installer
Author - David Whedon
*/
#include <ctype.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <debconfclient.h>
#include "utils.h"
#include "netcfg.h"
static char *interface = NULL;
static char *hostname = NULL;
static char *domain = NULL;
static u_int32_t ipaddress = 0;
static u_int32_t nameservers[4] = { 0 };
static struct debconfclient *client;
static char *dhcp_hostname = NULL;
char *
debconf_input (char *priority, char *template)
{
client->command (client, "fset", template, "seen", "false", NULL);
client->command (client, "input", priority, template, NULL);
client->command (client, "go", NULL);
client->command (client, "get", template, NULL);
return client->value;
}
static void
netcfg_get_dhcp ()
{
if (dhcp_hostname)
free (dhcp_hostname);
client->command (client, "input", "high", "netcfg/dhcp_hostname", NULL);
client->command (client, "go", NULL);
client->command (client, "get", "netcfg/dhcp_hostname", NULL);
if (client->value)
dhcp_hostname = strdup (client->value);
}
static void
netcfg_write_dhcp ()
{
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);
}
netcfg_mkdir (DHCPCD_DIR);
if ((fp = file_open (DHCPCD_FILE)))
{
fprintf (fp,
"\n# dhcpcd configuration: created during the Debian installation\n");
fprintf (fp, "IFACE=%s\n", interface);
if (dhcp_hostname)
fprintf (fp, "OPTIONS='-h %s'\n", dhcp_hostname);
}
}
static void
netcfg_activate_dhcp ()
{
char buf[128];
char *ptr;
execlog ("/sbin/ifconfig lo 127.0.0.1");
ptr = buf;
ptr += snprintf (buf, sizeof (buf), "/sbin/dhcpcd-2.2.x");
if (dhcp_hostname)
ptr +=
snprintf (ptr, sizeof (buf) - (ptr - buf), " -h %s", dhcp_hostname);
ptr += snprintf (ptr, sizeof (buf) - (ptr - buf), " %s", interface);
if (execlog (buf))
netcfg_die (client);
}
int
main (int argc, char *argv[])
{
char *ptr;
int finished = 1;
client = debconfclient_new ();
client->command (client, "title", "DHCP Network Configuration", NULL);
do
{
netcfg_get_common (client, interface, hostname, domain, nameservers);
netcfg_get_dhcp ();
client->command (client, "subst", "netcfg/confirm_dhcp", "interface",
interface, NULL);
client->command (client, "subst", "netcfg/confirm_dhcp", "hostname",
hostname, NULL);
client->command (client, "subst", "netcfg/confirm_dhcp", "domain",
(domain ? domain : "<none>"), NULL);
client->command (client, "subst", "netcfg/confirm_dhcp",
"dhcp_hostname",
(dhcp_hostname ? dhcp_hostname : "<none>"), NULL);
ptr = debconf_input ("medium", "netcfg/confirm_dhcp");
if (strstr (ptr, "true"))
finished = 1;
}
while (!finished);
netcfg_write_dhcp ();
netcfg_write_common (ipaddress, domain, hostname, nameservers);
debconf_input ("medium", "netcfg/do_dhcp");
netcfg_activate_dhcp ();
return 0;
}

177
netcfg-static.c

@ -0,0 +1,177 @@
/*
netcfg-static.c - Configure a static network for the debian-installer
Author - David Whedon
*/
#include <ctype.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <debconfclient.h>
#include "utils.h"
#include "netcfg.h"
static char *interface = NULL;
static char *hostname = NULL;
static char *domain = NULL;
static u_int32_t ipaddress = 0;
static u_int32_t nameservers[4] = { 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 struct debconfclient *client;
char *
debconf_input (char *priority, char *template)
{
client->command (client, "fset", template, "seen", "false", NULL);
client->command (client, "input", priority, template, NULL);
client->command (client, "go", NULL);
client->command (client, "get", template, NULL);
return client->value;
}
static void
netcfg_get_static ()
{
char *ptr;
ipaddress = network = broadcast = netmask = gateway = 0;
ptr = debconf_input ("critical", "netcfg/get_ipaddress");
dot2num (&ipaddress, ptr);
client->command (client, "subst", "netcfg/confirm_static",
"ipaddress",
(ipaddress ? num2dot (ipaddress) : "<none>"), NULL);
ptr = debconf_input ("critical", "netcfg/get_netmask");
dot2num (&netmask, ptr);
client->command (client, "subst", "netcfg/confirm_static",
"netmask", (netmask ? num2dot (netmask) : "<none>"), NULL);
network = ipaddress & netmask;
ptr = debconf_input ("critical", "netcfg/get_gateway");
dot2num (&gateway, ptr);
client->command (client, "subst", "netcfg/confirm_static",
"gateway", (gateway ? num2dot (gateway) : "<none>"), NULL);
if (gateway && ((gateway & netmask) != network))
{
client->command (client, "input", "high",
"netcfg/gateway_unreachable", NULL);
client->command (client, "go", NULL);
}
broadcast = (network | ~netmask);
}
static int
netcfg_write_static ()
{
FILE *fp;
if ((fp = file_open (NETWORKS_FILE)))
{
fprintf (fp, "localnet %s\n", num2dot (network));
fclose (fp);
}
else
goto error;
if ((fp = file_open (INTERFACES_FILE)))
{
fprintf (fp,
"\n# This entry was created during the Debian installation\n");
fprintf (fp, "# (network, broadcast and gateway are optional)\n");
fprintf (fp, "iface %s inet static\n", interface);
fprintf (fp, "\taddress %s\n", num2dot (ipaddress));
fprintf (fp, "\tnetmask %s\n", num2dot (netmask));
fprintf (fp, "\tnetwork %s\n", num2dot (network));
fprintf (fp, "\tbroadcast %s\n", num2dot (broadcast));
fprintf (fp, "\tgateway %s\n", num2dot (gateway));
fclose (fp);
}
else
goto error;
return 0;
error:
return -1;
}
static int
netcfg_activate_static ()
{
int rv;
char *ptr;
char buf[128];
execlog ("/sbin/ifconfig lo 127.0.0.1");
ptr = buf;
ptr +=
snprintf (buf, sizeof (buf), "/sbin/ifconfig %s %s", interface,
num2dot (ipaddress));
ptr +=
snprintf (ptr, sizeof (buf) - (ptr - buf), " netmask %s",
num2dot (netmask));
ptr +=
snprintf (ptr, sizeof (buf) - (ptr - buf), " broadcast %s",
num2dot (broadcast));
rv = execlog (buf);
if (rv != 0)
{
client->command (client, "input", "critical", "netcfg/error_cfg", NULL);
client->command (client, "go", NULL);
}
return 0;
}
int
main (int argc, char *argv[])
{
int finished = 0;
char *ptr;
client = debconfclient_new ();
client->command (client, "title", "Static Network Configuration", NULL);
do
{
netcfg_get_common (client, interface, hostname, domain, nameservers);
client->command (client, "subst", "netcfg/confirm_static",
"hostname", hostname, NULL);
client->command (client, "subst", "netcfg/confirm_static", "domain",
(domain ? domain : "<none>"), NULL);
netcfg_get_static ();
ptr = debconf_input ("medium", "netcfg/confirm_static");
if (strstr (ptr, "true"))
finished = 1;
}
while (!finished);
netcfg_write_common (ipaddress, domain, hostname, nameservers);
netcfg_write_static ();
netcfg_activate_static ();
return 0;
}

323
netcfg.c

@ -16,26 +16,11 @@
#include <sys/stat.h>
#include <debconfclient.h>
#include "utils.h"
#include "netcfg.h"
#define ETC_DIR "/etc"
#define NETWORK_DIR "/etc/network"
#define DHCPCD_DIR "/etc/dhcpc"
#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"
static char *interface = NULL;
static char *hostname = NULL;
static char *domain = NULL;
static u_int32_t ipaddress = 0;
static u_int32_t nameservers[4] = { 0 };
static struct debconfclient *client;
char *
debconf_input (char *priority, char *template)
static char *
debconf_input (struct debconfclient *client, char *priority, char *template)
{
client->command (client, "fset", template, "seen", "false", NULL);
client->command (client, "input", priority, template, NULL);
@ -45,7 +30,7 @@ debconf_input (char *priority, char *template)
}
static int
int
netcfg_mkdir (char *path)
{
if (check_dir (path) == -1)
@ -81,7 +66,7 @@ int_up_done:
}
static void
void
get_name (char *name, char *p)
{
while (isspace (*p))
@ -117,7 +102,7 @@ get_name (char *name, char *p)
static FILE *ifs = NULL;
static char ibuf[512];
static void
void
getif_start ()
{
if (ifs != NULL)
@ -134,7 +119,7 @@ getif_start ()
}
static char *
char *
getif (int all)
{
char rbuf[512];
@ -153,7 +138,7 @@ getif (int all)
}
static void
void
getif_end ()
{
if (ifs != NULL)
@ -165,7 +150,7 @@ getif_end ()
}
static char *
char *
get_ifdsc (const char *ifp)
{
int i;
@ -206,7 +191,7 @@ get_ifdsc (const char *ifp)
}
static FILE *
FILE *
file_open (char *path)
{
FILE *fp;
@ -221,7 +206,7 @@ file_open (char *path)
}
static void
void
dot2num (u_int32_t * num, char *dot)
{
char *p = dot - 1;
@ -258,7 +243,7 @@ error:
static char num2dot_buf[16];
static char *
char *
num2dot (u_int32_t num)
{
int byte[4];
@ -277,8 +262,8 @@ num2dot (u_int32_t num)
static void
netcfg_die ()
void
netcfg_die (struct debconfclient *client)
{
client->command (client, "input", "high", "netcfg/error", NULL);
client->command (client, "go", NULL);
@ -287,7 +272,7 @@ netcfg_die ()
static void
netcfg_get_interface ()
netcfg_get_interface (struct debconfclient *client, char *interface)
{
char *inter;
int len;
@ -302,7 +287,7 @@ netcfg_get_interface ()
}
if (!(ptr = malloc (128)))
netcfg_die ();
netcfg_die (client);
len = 128;
*ptr = '\0';
@ -335,13 +320,13 @@ netcfg_get_interface ()
client->command (client, "subst", "netcfg/choose_interface",
"ifchoices", ptr, NULL);
free (ptr);
inter = debconf_input ("high", "netcfg/choose_interface");
inter = debconf_input (client, "high", "netcfg/choose_interface");
if (!inter)
netcfg_die ();
netcfg_die (client);
}
else if (num_interfaces == 1)
inter = ptr;
inter = ptr;
/* grab just the interface name, not the description too */
interface = inter;
@ -360,17 +345,18 @@ error:
if (ptr)
free (ptr);
netcfg_die ();
netcfg_die (client);
}
static void
netcfg_get_common ()
void
netcfg_get_common (struct debconfclient *client, char *interface,
char *hostname, char *domain, u_int32_t nameservers[])
{
char *ptr, *ns;
netcfg_get_interface ();
netcfg_get_interface (client, interface);
if (hostname)
{
@ -378,7 +364,7 @@ netcfg_get_common ()
hostname = NULL;
}
hostname = strdup (debconf_input ("medium", "netcfg/get_hostname"));
hostname = strdup (debconf_input (client, "medium", "netcfg/get_hostname"));
if (domain)
{
@ -386,10 +372,10 @@ netcfg_get_common ()
domain = NULL;
}
if ((ptr = debconf_input ("medium", "netcfg/get_domain")))
if ((ptr = debconf_input (client, "medium", "netcfg/get_domain")))
domain = strdup (ptr);
ptr = debconf_input ("medium", "netcfg/get_nameservers");
ptr = debconf_input (client, "medium", "netcfg/get_nameservers");
if (ptr)
{
@ -420,7 +406,8 @@ netcfg_get_common ()
}
void
netcfg_write_common ()
netcfg_write_common (u_int32_t ipaddress, char *domain, char *hostname,
u_int32_t nameservers[])
{
FILE *fp;
@ -456,255 +443,3 @@ netcfg_write_common ()
fclose (fp);
}
}
#ifdef DHCP
static char *dhcp_hostname = NULL;
static void
netcfg_get_dhcp ()
{
if (dhcp_hostname)
free (dhcp_hostname);
client->command (client, "input", "high", "netcfg/dhcp_hostname", NULL);
client->command (client, "go", NULL);
client->command (client, "get", "netcfg/dhcp_hostname", NULL);
if (client->value)
dhcp_hostname = strdup (client->value);
}
static void
netcfg_write_dhcp ()
{
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);
}
netcfg_mkdir (DHCPCD_DIR);
if ((fp = file_open (DHCPCD_FILE)))
{
fprintf (fp,
"\n# dhcpcd configuration: created during the Debian installation\n");
fprintf (fp, "IFACE=%s\n", interface);
if (dhcp_hostname)
fprintf (fp, "OPTIONS='-h %s'\n", dhcp_hostname);
}
}
static void
netcfg_activate_dhcp ()
{
char buf[128];
char *ptr;
execlog ("/sbin/ifconfig lo 127.0.0.1");
ptr = buf;
ptr += snprintf (buf, sizeof (buf), "/sbin/dhcpcd-2.2.x");
if (dhcp_hostname)
ptr +=
snprintf (ptr, sizeof (buf) - (ptr - buf), " -h %s", dhcp_hostname);
ptr += snprintf (ptr, sizeof (buf) - (ptr - buf), " %s", interface);
if (execlog (buf))
netcfg_die ();
}
int
main (int argc, char *argv[])
{
char *ptr;
int finished = 1;
client = debconfclient_new ();
client->command (client, "title", "DHCP Network Configuration", NULL);
do
{
netcfg_get_common ();
netcfg_get_dhcp ();
client->command (client, "subst", "netcfg/confirm_dhcp", "interface",
interface, NULL);
client->command (client, "subst", "netcfg/confirm_dhcp", "hostname",
hostname, NULL);
client->command (client, "subst", "netcfg/confirm_dhcp", "domain",
(domain ? domain : "<none>"), NULL);
client->command (client, "subst", "netcfg/confirm_dhcp",
"dhcp_hostname",
(dhcp_hostname ? dhcp_hostname : "<none>"), NULL);
ptr = debconf_input ("medium", "netcfg/confirm_dhcp");
if (strstr (ptr, "true"))
finished = 1;
}
while (!finished);
netcfg_write_dhcp ();
netcfg_write_common ();
debconf_input ("medium", "netcfg/do_dhcp");
netcfg_activate_dhcp ();
return 0;
}
#endif /* DHCP */
#ifdef STATIC
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 struct debconfclient *client;
static void
netcfg_get_static ()
{
char *ptr;
ipaddress = network = broadcast = netmask = gateway = 0;
ptr = debconf_input ("critical", "netcfg/get_ipaddress");
dot2num (&ipaddress, ptr);
client->command (client, "subst", "netcfg/confirm_static",
"ipaddress",
(ipaddress ? num2dot (ipaddress) : "<none>"), NULL);
ptr = debconf_input ("critical", "netcfg/get_netmask");
dot2num (&netmask, ptr);
client->command (client, "subst", "netcfg/confirm_static",
"netmask", (netmask ? num2dot (netmask) : "<none>"), NULL);
network = ipaddress & netmask;
ptr = debconf_input ("critical", "netcfg/get_gateway");
dot2num (&gateway, ptr);
client->command (client, "subst", "netcfg/confirm_static",
"gateway", (gateway ? num2dot (gateway) : "<none>"), NULL);
if (gateway && ((gateway & netmask) != network))
{
client->command (client, "input", "high",
"netcfg/gateway_unreachable", NULL);
client->command (client, "go", NULL);
}
broadcast = (network | ~netmask);
}
static int
netcfg_write_static ()
{
FILE *fp;
if ((fp = file_open (NETWORKS_FILE)))
{
fprintf (fp, "localnet %s\n", num2dot (network));
fclose (fp);
}
else
goto error;
if ((fp = file_open (INTERFACES_FILE)))
{
fprintf (fp,
"\n# This entry was created during the Debian installation\n");
fprintf (fp, "# (network, broadcast and gateway are optional)\n");
fprintf (fp, "iface %s inet static\n", interface);
fprintf (fp, "\taddress %s\n", num2dot (ipaddress));
fprintf (fp, "\tnetmask %s\n", num2dot (netmask));
fprintf (fp, "\tnetwork %s\n", num2dot (network));
fprintf (fp, "\tbroadcast %s\n", num2dot (broadcast));
fprintf (fp, "\tgateway %s\n", num2dot (gateway));
fclose (fp);
}
else
goto error;
return 0;
error:
return -1;
}
static int
netcfg_activate_static ()
{
int rv;
char *ptr;
char buf[128];
execlog ("/sbin/ifconfig lo 127.0.0.1");
ptr = buf;
ptr +=
snprintf (buf, sizeof (buf), "/sbin/ifconfig %s %s", interface,
num2dot (ipaddress));
ptr +=
snprintf (ptr, sizeof (buf) - (ptr - buf), " netmask %s",
num2dot (netmask));
ptr +=
snprintf (ptr, sizeof (buf) - (ptr - buf), " broadcast %s",
num2dot (broadcast));
rv = execlog (buf);
if (rv != 0)
{
client->command (client, "input", "critical", "netcfg/error_cfg", NULL);
client->command (client, "go", NULL);
}
return 0;
}
int
main (int argc, char *argv[])
{
int finished = 0;
char *ptr;
client = debconfclient_new ();
client->command (client, "title", "Static Network Configuration", NULL);
do
{
netcfg_get_common ();
client->command (client, "subst", "netcfg/confirm_static",
"hostname", hostname, NULL);
client->command (client, "subst", "netcfg/confirm_static", "domain",
(domain ? domain : "<none>"), NULL);
netcfg_get_static ();
ptr = debconf_input ("medium", "netcfg/confirm_static");
if (strstr (ptr, "true"))
finished = 1;
}
while (!finished);
netcfg_write_common ();
netcfg_write_static ();
netcfg_activate_static ();
return 0;
}
#endif /* STATIC */

42
netcfg.h

@ -0,0 +1,42 @@
#ifndef _NETCFG_H_
#define _NETCFG_H_
#include <sys/types.h>
#define ETC_DIR "/etc"
#define NETWORK_DIR "/etc/network"
#define DHCPCD_DIR "/etc/dhcpc"
#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"
extern int netcfg_mkdir (char *path);
extern int is_interface_up (char *inter);
extern void get_name (char *name, char *p);
extern void getif_start ();
extern void getif_end ();
extern char *get_ifdsc (const char *ifp);
extern FILE *file_open (char *path);
extern void dot2num (u_int32_t * num, char *dot);
extern char *num2dot (u_int32_t num);
extern void netcfg_die (struct debconfclient *client);
extern void netcfg_get_common (struct debconfclient *client, char *interface,
char *hostname, char *domain,
u_int32_t nameservers[]);
extern void netcfg_write_common (u_int32_t ipaddress, char *domain,
char *hostname, u_int32_t nameservers[]);
#endif /* _NETCFG_H_ */
Loading…
Cancel
Save